WPILibC++  2019.1.1-beta-4-30-g3635116
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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/Types.h>
13 
14 #include "frc/AnalogTrigger.h"
15 #include "frc/CounterBase.h"
16 #include "frc/ErrorBase.h"
17 #include "frc/smartdashboard/SendableBase.h"
18 
19 namespace frc {
20 
21 class DigitalGlitchFilter;
22 
33 class Counter : public ErrorBase, public SendableBase, public CounterBase {
34  public:
35  enum Mode {
36  kTwoPulse = 0,
37  kSemiperiod = 1,
38  kPulseLength = 2,
39  kExternalDirection = 3
40  };
41 
55  explicit Counter(Mode mode = kTwoPulse);
56 
67  explicit Counter(int channel);
68 
81  explicit Counter(DigitalSource* source);
82 
96  explicit Counter(std::shared_ptr<DigitalSource> source);
97 
108  explicit Counter(const AnalogTrigger& trigger);
109 
122  Counter(EncodingType encodingType, DigitalSource* upSource,
123  DigitalSource* downSource, bool inverted);
124 
137  Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
138  std::shared_ptr<DigitalSource> downSource, bool inverted);
139 
140  ~Counter() override;
141 
142  Counter(Counter&& rhs);
143  Counter& operator=(Counter&& rhs);
144 
151  void SetUpSource(int channel);
152 
161  void SetUpSource(AnalogTrigger* analogTrigger, AnalogTriggerType triggerType);
162 
171  void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
172  AnalogTriggerType triggerType);
173 
174  void SetUpSource(DigitalSource* source);
175 
183  void SetUpSource(std::shared_ptr<DigitalSource> source);
184 
192  void SetUpSource(DigitalSource& source);
193 
202  void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
203 
207  void ClearUpSource();
208 
215  void SetDownSource(int channel);
216 
225  void SetDownSource(AnalogTrigger* analogTrigger,
226  AnalogTriggerType triggerType);
227 
236  void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
237  AnalogTriggerType triggerType);
238 
246  void SetDownSource(DigitalSource* source);
247 
256  void SetDownSource(DigitalSource& source);
257 
258  void SetDownSource(std::shared_ptr<DigitalSource> source);
259 
268  void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
269 
273  void ClearDownSource();
274 
280  void SetUpDownCounterMode();
281 
289 
295  void SetSemiPeriodMode(bool highSemiPeriod);
296 
306  void SetPulseLengthMode(double threshold);
307 
317  void SetReverseDirection(bool reverseDirection);
318 
326  void SetSamplesToAverage(int samplesToAverage);
327 
337  int GetSamplesToAverage() const;
338 
339  int GetFPGAIndex() const;
340 
341  // CounterBase interface
348  int Get() const override;
349 
356  void Reset() override;
357 
366  double GetPeriod() const override;
367 
378  void SetMaxPeriod(double maxPeriod) override;
379 
397  void SetUpdateWhenEmpty(bool enabled);
398 
409  bool GetStopped() const override;
410 
416  bool GetDirection() const override;
417 
418  void InitSendable(SendableBuilder& builder) override;
419 
420  protected:
421  // Makes the counter count up.
422  std::shared_ptr<DigitalSource> m_upSource;
423 
424  // Makes the counter count down.
425  std::shared_ptr<DigitalSource> m_downSource;
426 
427  // The FPGA counter object
428  HAL_CounterHandle m_counter = HAL_kInvalidHandle;
429 
430  private:
431  int m_index = 0; // The index of this counter.
432 
433  friend class DigitalGlitchFilter;
434 };
435 
436 } // namespace frc
void SetUpDownCounterMode()
Set standard up / down counting mode on this counter.
void SetSemiPeriodMode(bool highSemiPeriod)
Set Semi-period mode on this counter.
void Reset() override
Reset the Counter to zero.
void ClearDownSource()
Disable the down counting source to the counter.
void SetUpdateWhenEmpty(bool enabled)
Select whether you want to continue updating the event timer output when there are no samples capture...
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
Counter(Mode mode=kTwoPulse)
Create an instance of a counter where no sources are selected.
void SetDownSource(int channel)
Set the down counting source to be a digital input channel.
void ClearUpSource()
Disable the up counting source to the counter.
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
Class to enable glitch filtering on a set of digital inputs.
Definition: DigitalGlitchFilter.h:32
Interface for counting the number of ticks on a digital input channel.
Definition: CounterBase.h:21
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:33
void SetSamplesToAverage(int samplesToAverage)
Set the Samples to Average which specifies the number of samples of the timer to average when calcula...
void SetPulseLengthMode(double threshold)
Configure the counter to count in up or down based on the length of the input pulse.
DigitalSource Interface.
Definition: DigitalSource.h:25
void SetUpSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on an up counting source.
bool GetStopped() const override
Determine if the clock is stopped.
void SetDownSourceEdge(bool risingEdge, bool fallingEdge)
Set the edge sensitivity on a down counting source.
Base class for most objects.
Definition: ErrorBase.h:74
Definition: SendableBase.h:19
Definition: AnalogTrigger.h:22
int Get() const override
Read the current counter value.
Definition: SendableBuilder.h:23
void SetReverseDirection(bool reverseDirection)
Set the Counter to return reversed sensing on the direction.
void SetUpSource(int channel)
Set the upsource for the counter as a digital input channel.
double GetPeriod() const override
Get the Period of the most recent count.
void SetMaxPeriod(double maxPeriod) override
Set the maximum period where the device is still considered "moving".
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
bool GetDirection() const override
The last direction the counter value changed.
void SetExternalDirectionMode()
Set external direction mode on this counter.