2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-06-25 09:05:49 -07:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/AnalogInput.h"
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "wpi/hal/Types.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/util/sendable/Sendable.hpp"
|
|
|
|
|
#include "wpi/util/sendable/SendableHelper.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi {
|
2016-11-01 22:33:12 -07:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2014-06-12 18:07:45 -04:00
|
|
|
* Analog input class.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Connected to each analog channel is an averaging and oversampling engine.
|
2016-05-20 17:30:37 -07:00
|
|
|
* This engine accumulates the specified ( by SetAverageBits() and
|
2017-11-16 00:33:51 -08:00
|
|
|
* SetOversampleBits() ) number of samples before returning a new value. This is
|
|
|
|
|
* not a sliding window average. The only difference between the oversampled
|
2016-05-20 17:30:37 -07:00
|
|
|
* samples and the averaged samples is that the oversampled samples are simply
|
|
|
|
|
* accumulated effectively increasing the resolution, while the averaged samples
|
|
|
|
|
* are divided by the number of samples to retain the resolution, but get more
|
|
|
|
|
* stable values.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2025-11-07 20:00:05 -05:00
|
|
|
class AnalogInput : public wpi::util::Sendable,
|
|
|
|
|
public wpi::util::SendableHelper<AnalogInput> {
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Construct an analog input.
|
|
|
|
|
*
|
2026-03-20 19:48:53 -04:00
|
|
|
* @param channel The SmartIO channel to use.
|
2018-05-31 20:47:15 -07:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
explicit AnalogInput(int channel);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2019-08-25 18:42:00 -07:00
|
|
|
AnalogInput(AnalogInput&&) = default;
|
|
|
|
|
AnalogInput& operator=(AnalogInput&&) = default;
|
2018-09-24 00:08:25 -07:00
|
|
|
|
2024-09-07 13:58:15 -04:00
|
|
|
~AnalogInput() override = default;
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Get a sample straight from this channel.
|
|
|
|
|
*
|
2025-07-14 23:47:30 -07:00
|
|
|
* The sample is a 12-bit value representing the 0V to 3.3V range of the A/D
|
2018-05-31 20:47:15 -07:00
|
|
|
* converter in the module. The units are in A/D converter codes. Use
|
|
|
|
|
* GetVoltage() to get the analog value in calibrated units.
|
|
|
|
|
*
|
|
|
|
|
* @return A sample straight from this channel.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetValue() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a sample from the output of the oversample and average engine for this
|
|
|
|
|
* channel.
|
|
|
|
|
*
|
|
|
|
|
* The sample is 12-bit + the bits configured in SetOversampleBits().
|
|
|
|
|
* The value configured in SetAverageBits() will cause this value to be
|
|
|
|
|
* averaged 2**bits number of samples.
|
|
|
|
|
*
|
|
|
|
|
* This is not a sliding window. The sample will not change until
|
|
|
|
|
* 2**(OversampleBits + AverageBits) samples have been acquired from the
|
|
|
|
|
* module on this channel.
|
|
|
|
|
*
|
|
|
|
|
* Use GetAverageVoltage() to get the analog value in calibrated units.
|
|
|
|
|
*
|
|
|
|
|
* @return A sample from the oversample and average engine for this channel.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetAverageValue() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Get a scaled sample straight from this channel.
|
|
|
|
|
*
|
|
|
|
|
* The value is scaled to units of Volts using the calibrated scaling data
|
|
|
|
|
* from GetLSBWeight() and GetOffset().
|
|
|
|
|
*
|
|
|
|
|
* @return A scaled sample straight from this channel.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double GetVoltage() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a scaled sample from the output of the oversample and average engine
|
|
|
|
|
* for this channel.
|
|
|
|
|
*
|
|
|
|
|
* The value is scaled to units of Volts using the calibrated scaling data
|
|
|
|
|
* from GetLSBWeight() and GetOffset().
|
|
|
|
|
*
|
|
|
|
|
* Using oversampling will cause this value to be higher resolution, but it
|
|
|
|
|
* will update more slowly.
|
|
|
|
|
*
|
|
|
|
|
* Using averaging will cause this value to be more stable, but it will update
|
|
|
|
|
* more slowly.
|
|
|
|
|
*
|
|
|
|
|
* @return A scaled sample from the output of the oversample and average
|
|
|
|
|
* engine for this channel.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double GetAverageVoltage() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Get the channel number.
|
|
|
|
|
*
|
|
|
|
|
* @return The channel number.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetChannel() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Set the number of averaging bits.
|
|
|
|
|
*
|
|
|
|
|
* This sets the number of averaging bits. The actual number of averaged
|
|
|
|
|
* samples is 2^bits.
|
|
|
|
|
*
|
|
|
|
|
* Use averaging to improve the stability of your measurement at the expense
|
|
|
|
|
* of sampling rate. The averaging is done automatically in the FPGA.
|
|
|
|
|
*
|
|
|
|
|
* @param bits Number of bits of averaging.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
void SetAverageBits(int bits);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the number of averaging bits previously configured.
|
|
|
|
|
*
|
|
|
|
|
* This gets the number of averaging bits from the FPGA. The actual number of
|
|
|
|
|
* averaged samples is 2^bits. The averaging is done automatically in the
|
|
|
|
|
* FPGA.
|
|
|
|
|
*
|
|
|
|
|
* @return Number of bits of averaging previously configured.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetAverageBits() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the number of oversample bits.
|
|
|
|
|
*
|
|
|
|
|
* This sets the number of oversample bits. The actual number of oversampled
|
|
|
|
|
* values is 2^bits. Use oversampling to improve the resolution of your
|
|
|
|
|
* measurements at the expense of sampling rate. The oversampling is done
|
|
|
|
|
* automatically in the FPGA.
|
|
|
|
|
*
|
|
|
|
|
* @param bits Number of bits of oversampling.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
void SetOversampleBits(int bits);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the number of oversample bits previously configured.
|
|
|
|
|
*
|
|
|
|
|
* This gets the number of oversample bits from the FPGA. The actual number of
|
|
|
|
|
* oversampled values is 2^bits. The oversampling is done automatically in the
|
|
|
|
|
* FPGA.
|
|
|
|
|
*
|
|
|
|
|
* @return Number of bits of oversampling previously configured.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetOversampleBits() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Get the factory scaling least significant bit weight constant.
|
|
|
|
|
*
|
|
|
|
|
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
|
|
|
|
|
*
|
|
|
|
|
* @return Least significant bit weight.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetLSBWeight() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the factory scaling offset constant.
|
|
|
|
|
*
|
|
|
|
|
* Volts = ((LSB_Weight * 1e-9) * raw) - (Offset * 1e-9)
|
|
|
|
|
*
|
|
|
|
|
* @return Offset constant.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetOffset() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Set the sample rate per channel for all analog channels.
|
|
|
|
|
*
|
|
|
|
|
* The maximum rate is 500kS/s divided by the number of channels in use.
|
|
|
|
|
* This is 62500 samples/s per channel.
|
|
|
|
|
*
|
|
|
|
|
* @param samplesPerSecond The number of samples per second.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
static void SetSampleRate(double samplesPerSecond);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current sample rate for all channels
|
|
|
|
|
*
|
|
|
|
|
* @return Sample rate.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
static double GetSampleRate();
|
2014-07-21 16:32:36 -04:00
|
|
|
|
2019-10-04 22:56:24 -07:00
|
|
|
/**
|
|
|
|
|
* Indicates this input is used by a simulated device.
|
|
|
|
|
*
|
|
|
|
|
* @param device simulated device handle
|
|
|
|
|
*/
|
|
|
|
|
void SetSimDevice(HAL_SimDeviceHandle device);
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
void InitSendable(wpi::util::SendableBuilder& builder) override;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
2016-09-06 00:01:45 -07:00
|
|
|
int m_channel;
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::Handle<HAL_AnalogInputHandle, HAL_FreeAnalogInputPort> m_port;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi
|