WPILibC++  2020.3.2
Counter.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 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/Types.h>
13 
14 #include "frc/AnalogTrigger.h"
15 #include "frc/CounterBase.h"
16 #include "frc/ErrorBase.h"
17 #include "frc/smartdashboard/Sendable.h"
18 #include "frc/smartdashboard/SendableHelper.h"
19 
20 namespace frc {
21 
22 class DigitalGlitchFilter;
23 class SendableBuilder;
24 class DMA;
25 class DMASample;
26 
37 class Counter : public ErrorBase,
38  public CounterBase,
39  public Sendable,
40  public SendableHelper<Counter> {
41  friend class DMA;
42  friend class DMASample;
43 
44  public:
45  enum Mode {
46  kTwoPulse = 0,
47  kSemiperiod = 1,
48  kPulseLength = 2,
49  kExternalDirection = 3
50  };
51 
65  explicit Counter(Mode mode = kTwoPulse);
66 
77  explicit Counter(int channel);
78 
91  explicit Counter(DigitalSource* source);
92 
106  explicit Counter(std::shared_ptr<DigitalSource> source);
107 
118  explicit Counter(const AnalogTrigger& trigger);
119 
132  Counter(EncodingType encodingType, DigitalSource* upSource,
133  DigitalSource* downSource, bool inverted);
134 
147  Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
148  std::shared_ptr<DigitalSource> downSource, bool inverted);
149 
150  ~Counter() override;
151 
152  Counter(Counter&&) = default;
153  Counter& operator=(Counter&&) = default;
154 
161  void SetUpSource(int channel);
162 
171  void SetUpSource(AnalogTrigger* analogTrigger, AnalogTriggerType triggerType);
172 
181  void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
182  AnalogTriggerType triggerType);
183 
184  void SetUpSource(DigitalSource* source);
185 
193  void SetUpSource(std::shared_ptr<DigitalSource> source);
194 
202  void SetUpSource(DigitalSource& source);
203 
212  void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
213 
217  void ClearUpSource();
218 
225  void SetDownSource(int channel);
226 
235  void SetDownSource(AnalogTrigger* analogTrigger,
236  AnalogTriggerType triggerType);
237 
246  void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
247  AnalogTriggerType triggerType);
248 
256  void SetDownSource(DigitalSource* source);
257 
266  void SetDownSource(DigitalSource& source);
267 
268  void SetDownSource(std::shared_ptr<DigitalSource> source);
269 
278  void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
279 
283  void ClearDownSource();
284 
290  void SetUpDownCounterMode();
291 
299 
305  void SetSemiPeriodMode(bool highSemiPeriod);
306 
316  void SetPulseLengthMode(double threshold);
317 
327  void SetReverseDirection(bool reverseDirection);
328 
336  void SetSamplesToAverage(int samplesToAverage);
337 
347  int GetSamplesToAverage() const;
348 
349  int GetFPGAIndex() const;
350 
351  // CounterBase interface
358  int Get() const override;
359 
366  void Reset() override;
367 
376  double GetPeriod() const override;
377 
388  void SetMaxPeriod(double maxPeriod) override;
389 
407  void SetUpdateWhenEmpty(bool enabled);
408 
419  bool GetStopped() const override;
420 
426  bool GetDirection() const override;
427 
428  void InitSendable(SendableBuilder& builder) override;
429 
430  protected:
431  // Makes the counter count up.
432  std::shared_ptr<DigitalSource> m_upSource;
433 
434  // Makes the counter count down.
435  std::shared_ptr<DigitalSource> m_downSource;
436 
437  // The FPGA counter object
438  hal::Handle<HAL_CounterHandle> m_counter;
439 
440  private:
441  int m_index = 0; // The index of this counter.
442 
443  friend class DigitalGlitchFilter;
444 };
445 
446 } // 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:22
frc::Counter::ClearUpSource
void ClearUpSource()
Disable the up counting source to the counter.
frc::DMASample
Definition: DMASample.h:23
frc::Counter::SetSemiPeriodMode
void SetSemiPeriodMode(bool highSemiPeriod)
Set Semi-period mode on this counter.
frc::Counter::SetExternalDirectionMode
void SetExternalDirectionMode()
Set external direction mode on this counter.
frc::Counter::SetReverseDirection
void SetReverseDirection(bool reverseDirection)
Set the Counter to return reversed sensing on the direction.
frc::CounterBase
Interface for counting the number of ticks on a digital input channel.
Definition: CounterBase.h:21
frc::Counter::SetDownSource
void SetDownSource(int channel)
Set the down counting source to be a digital input channel.
frc::AnalogTrigger
Definition: AnalogTrigger.h:25
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(int channel)
Set the upsource for the counter as a digital input channel.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
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:33
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::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:17
frc::Counter::SetUpSourceEdge
void SetUpSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on an up counting source.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
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::DigitalSource
DigitalSource Interface.
Definition: DigitalSource.h:25
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::SendableBuilder
Definition: SendableBuilder.h:23
frc::Counter::GetDirection
bool GetDirection() const override
The last direction the counter value changed.
frc::Counter::SetMaxPeriod
void SetMaxPeriod(double maxPeriod) override
Set the maximum period where the device is still considered "moving".