WPILibC++  unspecified
Counter.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 <memory>
11 
12 #include <HAL/Counter.h>
13 #include <HAL/Types.h>
14 
15 #include "AnalogTrigger.h"
16 #include "CounterBase.h"
17 #include "SensorBase.h"
18 
19 namespace frc {
20 
22 
33 class Counter : public SensorBase, public CounterBase {
34  public:
35  enum Mode {
36  kTwoPulse = 0,
37  kSemiperiod = 1,
38  kPulseLength = 2,
39  kExternalDirection = 3
40  };
41 
42  explicit Counter(Mode mode = kTwoPulse);
43  explicit Counter(int channel);
44  explicit Counter(DigitalSource* source);
45  explicit Counter(std::shared_ptr<DigitalSource> source);
46  explicit Counter(const AnalogTrigger& trigger);
47  Counter(EncodingType encodingType, DigitalSource* upSource,
48  DigitalSource* downSource, bool inverted);
49  Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
50  std::shared_ptr<DigitalSource> downSource, bool inverted);
51  ~Counter() override;
52 
53  void SetUpSource(int channel);
54  void SetUpSource(AnalogTrigger* analogTrigger, AnalogTriggerType triggerType);
55  void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
56  AnalogTriggerType triggerType);
57  void SetUpSource(DigitalSource* source);
58  void SetUpSource(std::shared_ptr<DigitalSource> source);
59  void SetUpSource(DigitalSource& source);
60  void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
61  void ClearUpSource();
62 
63  void SetDownSource(int channel);
64  void SetDownSource(AnalogTrigger* analogTrigger,
65  AnalogTriggerType triggerType);
66  void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
67  AnalogTriggerType triggerType);
68  void SetDownSource(DigitalSource* source);
69  void SetDownSource(std::shared_ptr<DigitalSource> source);
70  void SetDownSource(DigitalSource& source);
71  void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
72  void ClearDownSource();
73 
74  void SetUpDownCounterMode();
76  void SetSemiPeriodMode(bool highSemiPeriod);
77  void SetPulseLengthMode(double threshold);
78 
79  void SetReverseDirection(bool reverseDirection);
80 
81  // CounterBase interface
82  int Get() const override;
83  void Reset() override;
84  double GetPeriod() const override;
85  void SetMaxPeriod(double maxPeriod) override;
86  void SetUpdateWhenEmpty(bool enabled);
87  bool GetStopped() const override;
88  bool GetDirection() const override;
89 
90  void SetSamplesToAverage(int samplesToAverage);
91  int GetSamplesToAverage() const;
92  int GetFPGAIndex() const { return m_index; }
93 
94  void InitSendable(SendableBuilder& builder) override;
95 
96  protected:
97  // Makes the counter count up.
98  std::shared_ptr<DigitalSource> m_upSource;
99 
100  // Makes the counter count down.
101  std::shared_ptr<DigitalSource> m_downSource;
102 
103  // The FPGA counter object
104  HAL_CounterHandle m_counter = HAL_kInvalidHandle;
105 
106  private:
107  int m_index = 0; // The index of this counter.
108 
109  friend class DigitalGlitchFilter;
110 };
111 
112 } // namespace frc
int Get() const override
Read the current counter value.
Definition: Counter.cpp:486
void ClearDownSource()
Disable the down counting source to the counter.
Definition: Counter.cpp:385
void SetDownSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on a down counting source.
Definition: Counter.cpp:370
Definition: RobotController.cpp:14
Counter(Mode mode=kTwoPulse)
Create an instance of a counter where no sources are selected.
Definition: Counter.cpp:32
Base class for all sensors.
Definition: SensorBase.h:25
bool GetDirection() const override
The last direction the counter value changed.
Definition: Counter.cpp:587
Class to enable glitch filtering on a set of digital inputs.
Definition: DigitalGlitchFilter.h:31
~Counter() override
Delete the Counter object.
Definition: Counter.cpp:168
void Reset() override
Reset the Counter to zero.
Definition: Counter.cpp:500
Interface for counting the number of ticks on a digital input channel.
Definition: CounterBase.h:21
void SetUpSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on an up counting source.
Definition: Counter.cpp:261
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:33
void SetExternalDirectionMode()
Set external direction mode on this counter.
Definition: Counter.cpp:411
void SetDownSource(int channel)
Set the down counting source to be a digital input channel.
Definition: Counter.cpp:290
void SetUpdateWhenEmpty(bool enabled)
Select whether you want to continue updating the event timer output when there are no samples capture...
Definition: Counter.cpp:557
DigitalSource Interface.
Definition: DigitalSource.h:25
void SetPulseLengthMode(double threshold)
Configure the counter to count in up or down based on the length of the input pulse.
Definition: Counter.cpp:439
Definition: AnalogTrigger.h:21
void SetSamplesToAverage(int samplesToAverage)
Set the Samples to Average which specifies the number of samples of the timer to average when calcula...
Definition: Counter.cpp:469
void SetUpDownCounterMode()
Set standard up / down counting mode on this counter.
Definition: Counter.cpp:398
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
Definition: Counter.cpp:610
Definition: SendableBuilder.h:23
double GetPeriod() const override
Get the Period of the most recent count.
Definition: Counter.cpp:515
void SetReverseDirection(bool reverseDirection)
Set the Counter to return reversed sensing on the direction.
Definition: Counter.cpp:603
void SetUpSource(int channel)
Set the upsource for the counter as a digital input channel.
Definition: Counter.cpp:183
bool GetStopped() const override
Determine if the clock is stopped.
Definition: Counter.cpp:574
void SetMaxPeriod(double maxPeriod) override
Set the maximum period where the device is still considered "moving".
Definition: Counter.cpp:533
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
Definition: Counter.cpp:455
void SetSemiPeriodMode(bool highSemiPeriod)
Set Semi-period mode on this counter.
Definition: Counter.cpp:423
void ClearUpSource()
Disable the up counting source to the counter.
Definition: Counter.cpp:276