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 scaled sample straight from this channel.
|
|
|
|
|
*
|
2026-04-17 19:38:25 -07:00
|
|
|
* The value is scaled to units of Volts.
|
2018-05-31 20:47:15 -07:00
|
|
|
*
|
|
|
|
|
* @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 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
|
|
|
|
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
|