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>
|
|
|
|
|
|
2024-09-07 13:58:15 -04:00
|
|
|
#include <hal/AnalogInput.h>
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/Types.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/Sendable.h>
|
|
|
|
|
#include <wpi/sendable/SendableHelper.h>
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2019-11-14 22:52:34 -08:00
|
|
|
class DMA;
|
|
|
|
|
class DMASample;
|
2019-09-14 15:22:54 -05: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
|
|
|
*/
|
2021-06-13 16:38:05 -07:00
|
|
|
class AnalogInput : public wpi::Sendable,
|
|
|
|
|
public wpi::SendableHelper<AnalogInput> {
|
2016-06-27 21:32:30 -07:00
|
|
|
friend class AnalogTrigger;
|
2016-07-07 21:31:45 -07:00
|
|
|
friend class AnalogGyro;
|
2019-11-14 22:52:34 -08:00
|
|
|
friend class DMA;
|
|
|
|
|
friend class DMASample;
|
2016-06-27 21:32:30 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
2017-11-19 19:04:28 -08:00
|
|
|
static constexpr int kAccumulatorModuleNumber = 1;
|
|
|
|
|
static constexpr int kAccumulatorNumChannels = 2;
|
|
|
|
|
static constexpr int kAccumulatorChannels[kAccumulatorNumChannels] = {0, 1};
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Construct an analog input.
|
|
|
|
|
*
|
|
|
|
|
* @param channel The channel number on the roboRIO to represent. 0-3 are
|
|
|
|
|
* on-board 4-7 are on the MXP port.
|
|
|
|
|
*/
|
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.
|
|
|
|
|
*
|
|
|
|
|
* The sample is a 12-bit value representing the 0V to 5V range of the A/D
|
|
|
|
|
* 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
|
|
|
/**
|
|
|
|
|
* Is the channel attached to an accumulator.
|
|
|
|
|
*
|
|
|
|
|
* @return The analog input is attached to an accumulator.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool IsAccumulatorChannel() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the accumulator.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void InitAccumulator();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set an initial value for the accumulator.
|
|
|
|
|
*
|
|
|
|
|
* This will be added to all values returned to the user.
|
|
|
|
|
*
|
2021-10-14 18:09:38 -07:00
|
|
|
* @param value The value that the accumulator should start from when reset.
|
2018-05-31 20:47:15 -07:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetAccumulatorInitialValue(int64_t value);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets the accumulator to the initial value.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void ResetAccumulator();
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the center value of the accumulator.
|
|
|
|
|
*
|
|
|
|
|
* The center value is subtracted from each A/D value before it is added to
|
|
|
|
|
* the accumulator. This is used for the center value of devices like gyros
|
|
|
|
|
* and accelerometers to take the device offset into account when integrating.
|
|
|
|
|
*
|
|
|
|
|
* This center value is based on the output of the oversampled and averaged
|
|
|
|
|
* source from the accumulator channel. Because of this, any non-zero
|
|
|
|
|
* oversample bits will affect the size of the value for this field.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
void SetAccumulatorCenter(int center);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the accumulator's deadband.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
void SetAccumulatorDeadband(int deadband);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the accumulated value.
|
|
|
|
|
*
|
|
|
|
|
* Read the value that has been accumulating.
|
|
|
|
|
* The accumulator is attached after the oversample and average engine.
|
|
|
|
|
*
|
|
|
|
|
* @return The 64-bit value accumulated since the last Reset().
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int64_t GetAccumulatorValue() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the number of accumulated values.
|
|
|
|
|
*
|
|
|
|
|
* Read the count of the accumulated values since the accumulator was last
|
|
|
|
|
* Reset().
|
|
|
|
|
*
|
|
|
|
|
* @return The number of times samples from the channel were accumulated.
|
|
|
|
|
*/
|
2016-07-12 10:45:14 -07:00
|
|
|
int64_t GetAccumulatorCount() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the accumulated value and the number of accumulated values atomically.
|
|
|
|
|
*
|
|
|
|
|
* This function reads the value and count from the FPGA atomically.
|
|
|
|
|
* This can be used for averaging.
|
|
|
|
|
*
|
|
|
|
|
* @param value Reference to the 64-bit accumulated output.
|
|
|
|
|
* @param count Reference to the number of accumulation cycles.
|
|
|
|
|
*/
|
2016-07-12 10:45:14 -07:00
|
|
|
void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
|
2014-05-02 17:54:01 -04: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);
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void InitSendable(wpi::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;
|
2024-09-07 13:58:15 -04:00
|
|
|
hal::Handle<HAL_AnalogInputHandle, HAL_FreeAnalogInputPort> m_port;
|
2015-06-25 15:07:55 -04:00
|
|
|
int64_t m_accumulatorOffset;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|