WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Counter.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2017. 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 #include <string>
12 
13 #include "AnalogTrigger.h"
14 #include "CounterBase.h"
15 #include "HAL/Counter.h"
16 #include "HAL/Types.h"
17 #include "LiveWindow/LiveWindowSendable.h"
18 #include "SensorBase.h"
19 
20 namespace frc {
21 
23 
34 class Counter : public SensorBase,
35  public CounterBase,
36  public LiveWindowSendable {
37  public:
38  enum Mode {
39  kTwoPulse = 0,
40  kSemiperiod = 1,
41  kPulseLength = 2,
42  kExternalDirection = 3
43  };
44  explicit Counter(Mode mode = kTwoPulse);
45  explicit Counter(int channel);
46  explicit Counter(DigitalSource* source);
47  explicit Counter(std::shared_ptr<DigitalSource> source);
48  explicit Counter(const AnalogTrigger& trigger);
49  Counter(EncodingType encodingType, DigitalSource* upSource,
50  DigitalSource* downSource, bool inverted);
51  Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
52  std::shared_ptr<DigitalSource> downSource, bool inverted);
53  virtual ~Counter();
54 
55  void SetUpSource(int channel);
56  void SetUpSource(AnalogTrigger* analogTrigger, AnalogTriggerType triggerType);
57  void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
58  AnalogTriggerType triggerType);
59  void SetUpSource(DigitalSource* source);
60  void SetUpSource(std::shared_ptr<DigitalSource> source);
61  void SetUpSource(DigitalSource& source);
62  void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
63  void ClearUpSource();
64 
65  void SetDownSource(int channel);
66  void SetDownSource(AnalogTrigger* analogTrigger,
67  AnalogTriggerType triggerType);
68  void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
69  AnalogTriggerType triggerType);
70  void SetDownSource(DigitalSource* source);
71  void SetDownSource(std::shared_ptr<DigitalSource> source);
72  void SetDownSource(DigitalSource& source);
73  void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
74  void ClearDownSource();
75 
76  void SetUpDownCounterMode();
78  void SetSemiPeriodMode(bool highSemiPeriod);
79  void SetPulseLengthMode(double threshold);
80 
81  void SetReverseDirection(bool reverseDirection);
82 
83  // CounterBase interface
84  int Get() const override;
85  void Reset() override;
86  double GetPeriod() const override;
87  void SetMaxPeriod(double maxPeriod) override;
88  void SetUpdateWhenEmpty(bool enabled);
89  bool GetStopped() const override;
90  bool GetDirection() const override;
91 
92  void SetSamplesToAverage(int samplesToAverage);
93  int GetSamplesToAverage() const;
94  int GetFPGAIndex() const { return m_index; }
95 
96  void UpdateTable() override;
97  void StartLiveWindowMode() override;
98  void StopLiveWindowMode() override;
99  std::string GetSmartDashboardType() const override;
100  void InitTable(std::shared_ptr<ITable> subTable) override;
101  std::shared_ptr<ITable> GetTable() const override;
102 
103  protected:
104  // Makes the counter count up.
105  std::shared_ptr<DigitalSource> m_upSource;
106  // Makes the counter count down.
107  std::shared_ptr<DigitalSource> m_downSource;
108  // The FPGA counter object
109  HAL_CounterHandle m_counter = HAL_kInvalidHandle;
110 
111  private:
112  int m_index = 0;
113 
114  std::shared_ptr<ITable> m_table;
115  friend class DigitalGlitchFilter;
116 };
117 
118 } // namespace frc
int Get() const override
Read the current counter value.
Definition: Counter.cpp:480
void ClearDownSource()
Disable the down counting source to the counter.
Definition: Counter.cpp:379
void SetDownSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on a down counting source.
Definition: Counter.cpp:364
Counter(Mode mode=kTwoPulse)
Create an instance of a counter where no sources are selected.
Definition: Counter.cpp:30
Live Window Sendable is a special type of object sendable to the live window.
Definition: LiveWindowSendable.h:17
Base class for all sensors.
Definition: SensorBase.h:20
void UpdateTable() override
Update the table for this sendable object with the latest values.
Definition: Counter.cpp:604
bool GetDirection() const override
The last direction the counter value changed.
Definition: Counter.cpp:581
Class to enable glitch filtering on a set of digital inputs.
Definition: DigitalGlitchFilter.h:28
void Reset() override
Reset the Counter to zero.
Definition: Counter.cpp:494
std::string GetSmartDashboardType() const override
Definition: Counter.cpp:614
Interface for counting the number of ticks on a digital input channel.
Definition: CounterBase.h:20
void SetUpSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on an up counting source.
Definition: Counter.cpp:256
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:34
void SetExternalDirectionMode()
Set external direction mode on this counter.
Definition: Counter.cpp:405
void StopLiveWindowMode() override
Stop having this sendable object automatically respond to value changes.
Definition: Counter.cpp:612
void SetDownSource(int channel)
Set the down counting source to be a digital input channel.
Definition: Counter.cpp:285
void SetUpdateWhenEmpty(bool enabled)
Select whether you want to continue updating the event timer output when there are no samples capture...
Definition: Counter.cpp:551
DigitalSource Interface.
Definition: DigitalSource.h:25
virtual ~Counter()
Delete the Counter object.
Definition: Counter.cpp:164
void StartLiveWindowMode() override
Start having this sendable object automatically respond to value changes reflect the value on the tab...
Definition: Counter.cpp:610
void SetPulseLengthMode(double threshold)
Configure the counter to count in up or down based on the length of the input pulse.
Definition: Counter.cpp:433
Definition: AnalogTrigger.h:20
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:463
void SetUpDownCounterMode()
Set standard up / down counting mode on this counter.
Definition: Counter.cpp:392
std::shared_ptr< ITable > GetTable() const override
Definition: Counter.cpp:621
double GetPeriod() const override
Get the Period of the most recent count.
Definition: Counter.cpp:509
void SetReverseDirection(bool reverseDirection)
Set the Counter to return reversed sensing on the direction.
Definition: Counter.cpp:597
void InitTable(std::shared_ptr< ITable > subTable) override
Initializes a table for this sendable object.
Definition: Counter.cpp:616
void SetUpSource(int channel)
Set the upsource for the counter as a digital input channel.
Definition: Counter.cpp:179
bool GetStopped() const override
Determine if the clock is stopped.
Definition: Counter.cpp:568
void SetMaxPeriod(double maxPeriod) override
Set the maximum period where the device is still considered "moving".
Definition: Counter.cpp:527
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
Definition: Counter.cpp:449
void SetSemiPeriodMode(bool highSemiPeriod)
Set Semi-period mode on this counter.
Definition: Counter.cpp:417
void ClearUpSource()
Disable the up counting source to the counter.
Definition: Counter.cpp:271