WPILibC++  unspecified
AnalogInput.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <stdint.h>
11 
12 #include <HAL/Types.h>
13 
14 #include "PIDSource.h"
15 #include "SensorBase.h"
16 
17 namespace frc {
18 
31 class AnalogInput : public SensorBase, public PIDSource {
32  friend class AnalogTrigger;
33  friend class AnalogGyro;
34 
35  public:
36  static constexpr int kAccumulatorModuleNumber = 1;
37  static constexpr int kAccumulatorNumChannels = 2;
38  static constexpr int kAccumulatorChannels[kAccumulatorNumChannels] = {0, 1};
39 
40  explicit AnalogInput(int channel);
41  ~AnalogInput() override;
42 
43  int GetValue() const;
44  int GetAverageValue() const;
45 
46  double GetVoltage() const;
47  double GetAverageVoltage() const;
48 
49  int GetChannel() const;
50 
51  void SetAverageBits(int bits);
52  int GetAverageBits() const;
53  void SetOversampleBits(int bits);
54  int GetOversampleBits() const;
55 
56  int GetLSBWeight() const;
57  int GetOffset() const;
58 
59  bool IsAccumulatorChannel() const;
60  void InitAccumulator();
61  void SetAccumulatorInitialValue(int64_t value);
62  void ResetAccumulator();
63  void SetAccumulatorCenter(int center);
64  void SetAccumulatorDeadband(int deadband);
65  int64_t GetAccumulatorValue() const;
66  int64_t GetAccumulatorCount() const;
67  void GetAccumulatorOutput(int64_t& value, int64_t& count) const;
68 
69  static void SetSampleRate(double samplesPerSecond);
70  static double GetSampleRate();
71 
72  double PIDGet() override;
73 
74  void InitSendable(SendableBuilder& builder) override;
75 
76  private:
77  int m_channel;
78  // TODO: Adjust HAL to avoid use of raw pointers.
79  HAL_AnalogInputHandle m_port;
80  int64_t m_accumulatorOffset;
81 };
82 
83 } // namespace frc
void SetOversampleBits(int bits)
Set the number of oversample bits.
Definition: AnalogInput.cpp:219
Definition: RobotController.cpp:14
static double GetSampleRate()
Get the current sample rate for all channels.
Definition: AnalogInput.cpp:395
int GetOffset() const
Get the factory scaling offset constant.
Definition: AnalogInput.cpp:158
double GetAverageVoltage() const
Get a scaled sample from the output of the oversample and average engine for this channel...
Definition: AnalogInput.cpp:128
Base class for all sensors.
Definition: SensorBase.h:25
int GetLSBWeight() const
Get the factory scaling least significant bit weight constant.
Definition: AnalogInput.cpp:143
void SetAccumulatorCenter(int center)
Set the center value of the accumulator.
Definition: AnalogInput.cpp:310
int GetOversampleBits() const
Get the number of oversample bits previously configured.
Definition: AnalogInput.cpp:235
double GetVoltage() const
Get a scaled sample straight from this channel.
Definition: AnalogInput.cpp:106
void SetAccumulatorInitialValue(int64_t value)
Set an initial value for the accumulator.
Definition: AnalogInput.cpp:275
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
~AnalogInput() override
Channel destructor.
Definition: AnalogInput.cpp:54
int GetAverageValue() const
Get a sample from the output of the oversample and average engine for this channel.
Definition: AnalogInput.cpp:90
AnalogInput(int channel)
Construct an analog input.
Definition: AnalogInput.cpp:27
int GetAverageBits() const
Get the number of averaging bits previously configured.
Definition: AnalogInput.cpp:202
static void SetSampleRate(double samplesPerSecond)
Set the sample rate per channel for all analog channels.
Definition: AnalogInput.cpp:384
int64_t GetAccumulatorValue() const
Read the accumulated value.
Definition: AnalogInput.cpp:335
int64_t GetAccumulatorCount() const
Read the number of accumulated values.
Definition: AnalogInput.cpp:351
int GetChannel() const
Get the channel number.
Definition: AnalogInput.cpp:171
int GetValue() const
Get a sample straight from this channel.
Definition: AnalogInput.cpp:68
Use a rate gyro to return the robots heading relative to a starting position.
Definition: AnalogGyro.h:33
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
Definition: AnalogInput.cpp:412
Definition: AnalogTrigger.h:21
void ResetAccumulator()
Resets the accumulator to the initial value.
Definition: AnalogInput.cpp:283
bool IsAccumulatorChannel() const
Is the channel attached to an accumulator.
Definition: AnalogInput.cpp:248
Definition: SendableBuilder.h:23
void SetAccumulatorDeadband(int deadband)
Set the accumulator&#39;s deadband.
Definition: AnalogInput.cpp:320
void InitAccumulator()
Initialize the accumulator.
Definition: AnalogInput.cpp:259
void GetAccumulatorOutput(int64_t &value, int64_t &count) const
Read the accumulated value and the number of accumulated values atomically.
Definition: AnalogInput.cpp:368
void SetAverageBits(int bits)
Set the number of averaging bits.
Definition: AnalogInput.cpp:187
Analog input class.
Definition: AnalogInput.h:31
double PIDGet() override
Get the Average value for the PID Source base object.
Definition: AnalogInput.cpp:407