WPILibC++  unspecified
Counter.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2017 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 #include <string>
12 
13 #include <HAL/Counter.h>
14 #include <HAL/Types.h>
15 
16 #include "AnalogTrigger.h"
17 #include "CounterBase.h"
18 #include "LiveWindow/LiveWindowSendable.h"
19 #include "SensorBase.h"
20 #include "networktables/NetworkTableEntry.h"
21 
22 namespace frc {
23 
25 
36 class Counter : public SensorBase,
37  public CounterBase,
38  public LiveWindowSendable {
39  public:
40  enum Mode {
41  kTwoPulse = 0,
42  kSemiperiod = 1,
43  kPulseLength = 2,
44  kExternalDirection = 3
45  };
46  explicit Counter(Mode mode = kTwoPulse);
47  explicit Counter(int channel);
48  explicit Counter(DigitalSource* source);
49  explicit Counter(std::shared_ptr<DigitalSource> source);
50  explicit Counter(const AnalogTrigger& trigger);
51  Counter(EncodingType encodingType, DigitalSource* upSource,
52  DigitalSource* downSource, bool inverted);
53  Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
54  std::shared_ptr<DigitalSource> downSource, bool inverted);
55  virtual ~Counter();
56 
57  void SetUpSource(int channel);
58  void SetUpSource(AnalogTrigger* analogTrigger, AnalogTriggerType triggerType);
59  void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
60  AnalogTriggerType triggerType);
61  void SetUpSource(DigitalSource* source);
62  void SetUpSource(std::shared_ptr<DigitalSource> source);
63  void SetUpSource(DigitalSource& source);
64  void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
65  void ClearUpSource();
66 
67  void SetDownSource(int channel);
68  void SetDownSource(AnalogTrigger* analogTrigger,
69  AnalogTriggerType triggerType);
70  void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
71  AnalogTriggerType triggerType);
72  void SetDownSource(DigitalSource* source);
73  void SetDownSource(std::shared_ptr<DigitalSource> source);
74  void SetDownSource(DigitalSource& source);
75  void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
76  void ClearDownSource();
77 
78  void SetUpDownCounterMode();
80  void SetSemiPeriodMode(bool highSemiPeriod);
81  void SetPulseLengthMode(double threshold);
82 
83  void SetReverseDirection(bool reverseDirection);
84 
85  // CounterBase interface
86  int Get() const override;
87  void Reset() override;
88  double GetPeriod() const override;
89  void SetMaxPeriod(double maxPeriod) override;
90  void SetUpdateWhenEmpty(bool enabled);
91  bool GetStopped() const override;
92  bool GetDirection() const override;
93 
94  void SetSamplesToAverage(int samplesToAverage);
95  int GetSamplesToAverage() const;
96  int GetFPGAIndex() const { return m_index; }
97 
98  void UpdateTable() override;
99  void StartLiveWindowMode() override;
100  void StopLiveWindowMode() override;
101  std::string GetSmartDashboardType() const override;
102  void InitTable(std::shared_ptr<nt::NetworkTable> subTable) override;
103 
104  protected:
105  // Makes the counter count up.
106  std::shared_ptr<DigitalSource> m_upSource;
107  // Makes the counter count down.
108  std::shared_ptr<DigitalSource> m_downSource;
109  // The FPGA counter object
110  HAL_CounterHandle m_counter = HAL_kInvalidHandle;
111 
112  private:
113  int m_index = 0;
114 
115  nt::NetworkTableEntry m_valueEntry;
116  friend class DigitalGlitchFilter;
117 };
118 
119 } // namespace frc
int Get() const override
Read the current counter value.
Definition: Counter.cpp:481
void ClearDownSource()
Disable the down counting source to the counter.
Definition: Counter.cpp:380
void SetDownSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on a down counting source.
Definition: Counter.cpp:365
Definition: Timer.cpp:18
Counter(Mode mode=kTwoPulse)
Create an instance of a counter where no sources are selected.
Definition: Counter.cpp:31
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:605
bool GetDirection() const override
The last direction the counter value changed.
Definition: Counter.cpp:582
void InitTable(std::shared_ptr< nt::NetworkTable > subTable) override
Initializes a table for this sendable object.
Definition: Counter.cpp:615
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:495
std::string GetSmartDashboardType() const override
Definition: Counter.cpp:613
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:257
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:36
void SetExternalDirectionMode()
Set external direction mode on this counter.
Definition: Counter.cpp:406
void StopLiveWindowMode() override
Stop having this sendable object automatically respond to value changes.
Definition: Counter.cpp:611
void SetDownSource(int channel)
Set the down counting source to be a digital input channel.
Definition: Counter.cpp:286
void SetUpdateWhenEmpty(bool enabled)
Select whether you want to continue updating the event timer output when there are no samples capture...
Definition: Counter.cpp:552
DigitalSource Interface.
Definition: DigitalSource.h:26
virtual ~Counter()
Delete the Counter object.
Definition: Counter.cpp:165
void StartLiveWindowMode() override
Start having this sendable object automatically respond to value changes reflect the value on the tab...
Definition: Counter.cpp:609
void SetPulseLengthMode(double threshold)
Configure the counter to count in up or down based on the length of the input pulse.
Definition: Counter.cpp:434
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:464
void SetUpDownCounterMode()
Set standard up / down counting mode on this counter.
Definition: Counter.cpp:393
NetworkTables Entry.
Definition: NetworkTableEntry.h:30
double GetPeriod() const override
Get the Period of the most recent count.
Definition: Counter.cpp:510
void SetReverseDirection(bool reverseDirection)
Set the Counter to return reversed sensing on the direction.
Definition: Counter.cpp:598
void SetUpSource(int channel)
Set the upsource for the counter as a digital input channel.
Definition: Counter.cpp:180
bool GetStopped() const override
Determine if the clock is stopped.
Definition: Counter.cpp:569
void SetMaxPeriod(double maxPeriod) override
Set the maximum period where the device is still considered "moving".
Definition: Counter.cpp:528
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
Definition: Counter.cpp:450
void SetSemiPeriodMode(bool highSemiPeriod)
Set Semi-period mode on this counter.
Definition: Counter.cpp:418
void ClearUpSource()
Disable the up counting source to the counter.
Definition: Counter.cpp:272