WPILibC++  2021.3.1
Counter.h
1 // Copyright (c) FIRST and other WPILib contributors.
2 // Open Source Software; you can modify and/or share it under the terms of
3 // the WPILib BSD license file in the root directory of this project.
4 
5 #pragma once
6 
7 #include <memory>
8 
9 #include <hal/Types.h>
10 
11 #include "frc/AnalogTrigger.h"
12 #include "frc/CounterBase.h"
13 #include "frc/ErrorBase.h"
14 #include "frc/smartdashboard/Sendable.h"
15 #include "frc/smartdashboard/SendableHelper.h"
16 
17 namespace frc {
18 
19 class DigitalGlitchFilter;
20 class SendableBuilder;
21 class DMA;
22 class DMASample;
23 
34 class Counter : public ErrorBase,
35  public CounterBase,
36  public Sendable,
37  public SendableHelper<Counter> {
38  friend class DMA;
39  friend class DMASample;
40 
41  public:
42  enum Mode {
43  kTwoPulse = 0,
44  kSemiperiod = 1,
45  kPulseLength = 2,
46  kExternalDirection = 3
47  };
48 
62  explicit Counter(Mode mode = kTwoPulse);
63 
74  explicit Counter(int channel);
75 
88  explicit Counter(DigitalSource* source);
89 
103  explicit Counter(std::shared_ptr<DigitalSource> source);
104 
115  explicit Counter(const AnalogTrigger& trigger);
116 
129  Counter(EncodingType encodingType, DigitalSource* upSource,
130  DigitalSource* downSource, bool inverted);
131 
144  Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
145  std::shared_ptr<DigitalSource> downSource, bool inverted);
146 
147  ~Counter() override;
148 
149  Counter(Counter&&) = default;
150  Counter& operator=(Counter&&) = default;
151 
158  void SetUpSource(int channel);
159 
168  void SetUpSource(AnalogTrigger* analogTrigger, AnalogTriggerType triggerType);
169 
178  void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
179  AnalogTriggerType triggerType);
180 
181  void SetUpSource(DigitalSource* source);
182 
190  void SetUpSource(std::shared_ptr<DigitalSource> source);
191 
199  void SetUpSource(DigitalSource& source);
200 
209  void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
210 
215 
222  void SetDownSource(int channel);
223 
232  void SetDownSource(AnalogTrigger* analogTrigger,
233  AnalogTriggerType triggerType);
234 
243  void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
244  AnalogTriggerType triggerType);
245 
254 
264 
265  void SetDownSource(std::shared_ptr<DigitalSource> source);
266 
275  void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
276 
281 
288 
296 
302  void SetSemiPeriodMode(bool highSemiPeriod);
303 
313  void SetPulseLengthMode(double threshold);
314 
324  void SetReverseDirection(bool reverseDirection);
325 
333  void SetSamplesToAverage(int samplesToAverage);
334 
344  int GetSamplesToAverage() const;
345 
346  int GetFPGAIndex() const;
347 
348  // CounterBase interface
355  int Get() const override;
356 
363  void Reset() override;
364 
373  double GetPeriod() const override;
374 
385  void SetMaxPeriod(double maxPeriod) final;
386 
404  void SetUpdateWhenEmpty(bool enabled);
405 
416  bool GetStopped() const override;
417 
423  bool GetDirection() const override;
424 
425  void InitSendable(SendableBuilder& builder) override;
426 
427  protected:
428  // Makes the counter count up.
429  std::shared_ptr<DigitalSource> m_upSource;
430 
431  // Makes the counter count down.
432  std::shared_ptr<DigitalSource> m_downSource;
433 
434  // The FPGA counter object
435  hal::Handle<HAL_CounterHandle> m_counter;
436 
437  private:
438  int m_index = 0; // The index of this counter.
439 
440  friend class DigitalGlitchFilter;
441 };
442 
443 } // namespace frc
frc::Counter::GetPeriod
double GetPeriod() const override
Get the Period of the most recent count.
frc::Counter::GetStopped
bool GetStopped() const override
Determine if the clock is stopped.
frc::DMA
Definition: DMA.h:19
frc::Counter::ClearUpSource
void ClearUpSource()
Disable the up counting source to the counter.
frc::DMASample
Definition: DMASample.h:20
frc::Counter::SetSemiPeriodMode
void SetSemiPeriodMode(bool highSemiPeriod)
Set Semi-period mode on this counter.
frc::Counter::Counter
Counter(EncodingType encodingType, std::shared_ptr< DigitalSource > upSource, std::shared_ptr< DigitalSource > downSource, bool inverted)
Create an instance of a Counter object.
frc::Counter::SetExternalDirectionMode
void SetExternalDirectionMode()
Set external direction mode on this counter.
frc::Counter::Counter
Counter(std::shared_ptr< DigitalSource > source)
Create an instance of a counter from a Digital Source (such as a Digital Input).
frc::Counter::SetReverseDirection
void SetReverseDirection(bool reverseDirection)
Set the Counter to return reversed sensing on the direction.
frc::Counter::Counter
Counter(EncodingType encodingType, DigitalSource *upSource, DigitalSource *downSource, bool inverted)
Create an instance of a Counter object.
frc::CounterBase
Interface for counting the number of ticks on a digital input channel.
Definition: CounterBase.h:18
frc::Counter::SetDownSource
void SetDownSource(int channel)
Set the down counting source to be a digital input channel.
frc::AnalogTrigger
Definition: AnalogTrigger.h:24
frc::Counter::InitSendable
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
frc::Counter::ClearDownSource
void ClearDownSource()
Disable the down counting source to the counter.
frc::Counter::Counter
Counter(Mode mode=kTwoPulse)
Create an instance of a counter where no sources are selected.
frc::Counter::SetUpSource
void SetUpSource(std::shared_ptr< DigitalSource > source)
Set the source object that causes the counter to count up.
frc::Counter::Counter
Counter(DigitalSource *source)
Create an instance of a counter from a Digital Source (such as a Digital Input).
frc::Counter::SetUpSource
void SetUpSource(DigitalSource &source)
Set the source object that causes the counter to count up.
frc::Counter::Counter
Counter(const AnalogTrigger &trigger)
Create an instance of a Counter object.
frc::Counter::SetUpSource
void SetUpSource(int channel)
Set the upsource for the counter as a digital input channel.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:99
frc::Counter::Reset
void Reset() override
Reset the Counter to zero.
frc::Counter::SetSamplesToAverage
void SetSamplesToAverage(int samplesToAverage)
Set the Samples to Average which specifies the number of samples of the timer to average when calcula...
frc::DigitalGlitchFilter
Class to enable glitch filtering on a set of digital inputs.
Definition: DigitalGlitchFilter.h:32
frc::Counter::SetDownSource
void SetDownSource(DigitalSource &source)
Set the source object that causes the counter to count down.
frc::Counter::SetUpdateWhenEmpty
void SetUpdateWhenEmpty(bool enabled)
Select whether you want to continue updating the event timer output when there are no samples capture...
frc::Counter::Counter
Counter(int channel)
Create an instance of a Counter object.
frc::Counter::SetDownSource
void SetDownSource(DigitalSource *source)
Set the source object that causes the counter to count down.
frc::Counter::SetUpSource
void SetUpSource(std::shared_ptr< AnalogTrigger > analogTrigger, AnalogTriggerType triggerType)
Set the up counting source to be an analog trigger.
frc::Counter::SetUpDownCounterMode
void SetUpDownCounterMode()
Set standard up / down counting mode on this counter.
frc::Counter::Get
int Get() const override
Read the current counter value.
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:14
frc::Counter::SetDownSource
void SetDownSource(std::shared_ptr< AnalogTrigger > analogTrigger, AnalogTriggerType triggerType)
Set the down counting source to be an analog trigger.
frc::Counter::SetUpSourceEdge
void SetUpSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on an up counting source.
frc
WPILib FRC namespace.
Definition: VisionRunner.inc:9
frc::Counter::SetUpSource
void SetUpSource(AnalogTrigger *analogTrigger, AnalogTriggerType triggerType)
Set the up counting source to be an analog trigger.
frc::Counter::SetDownSource
void SetDownSource(AnalogTrigger *analogTrigger, AnalogTriggerType triggerType)
Set the down counting source to be an analog trigger.
frc::Counter
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:37
frc::Counter::GetSamplesToAverage
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
frc::Counter::SetPulseLengthMode
void SetPulseLengthMode(double threshold)
Configure the counter to count in up or down based on the length of the input pulse.
frc::Counter::SetDownSourceEdge
void SetDownSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on a down counting source.
frc::Counter::SetMaxPeriod
void SetMaxPeriod(double maxPeriod) final
Set the maximum period where the device is still considered "moving".
frc::DigitalSource
DigitalSource Interface.
Definition: DigitalSource.h:22
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:25
frc::SendableBuilder
Definition: SendableBuilder.h:21
frc::Counter::GetDirection
bool GetDirection() const override
The last direction the counter value changed.