WPILibC++  2018.4.1-1217-gf0ac048
 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/Counter.h>
13 #include <hal/Types.h>
14 
15 #include "frc/AnalogTrigger.h"
16 #include "frc/CounterBase.h"
17 #include "frc/ErrorBase.h"
18 #include "frc/smartdashboard/SendableBase.h"
19 
20 namespace frc {
21 
22 class DigitalGlitchFilter;
23 
34 class Counter : public ErrorBase, public SendableBase, public CounterBase {
35  public:
36  enum Mode {
37  kTwoPulse = 0,
38  kSemiperiod = 1,
39  kPulseLength = 2,
40  kExternalDirection = 3
41  };
42 
56  explicit Counter(Mode mode = kTwoPulse);
57 
68  explicit Counter(int channel);
69 
82  explicit Counter(DigitalSource* source);
83 
97  explicit Counter(std::shared_ptr<DigitalSource> source);
98 
109  explicit Counter(const AnalogTrigger& trigger);
110 
123  Counter(EncodingType encodingType, DigitalSource* upSource,
124  DigitalSource* downSource, bool inverted);
125 
138  Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
139  std::shared_ptr<DigitalSource> downSource, bool inverted);
140 
141  ~Counter() override;
142 
143  Counter(Counter&& rhs);
144  Counter& operator=(Counter&& rhs);
145 
152  void SetUpSource(int channel);
153 
162  void SetUpSource(AnalogTrigger* analogTrigger, AnalogTriggerType triggerType);
163 
172  void SetUpSource(std::shared_ptr<AnalogTrigger> analogTrigger,
173  AnalogTriggerType triggerType);
174 
175  void SetUpSource(DigitalSource* source);
176 
184  void SetUpSource(std::shared_ptr<DigitalSource> source);
185 
193  void SetUpSource(DigitalSource& source);
194 
203  void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
204 
208  void ClearUpSource();
209 
216  void SetDownSource(int channel);
217 
226  void SetDownSource(AnalogTrigger* analogTrigger,
227  AnalogTriggerType triggerType);
228 
237  void SetDownSource(std::shared_ptr<AnalogTrigger> analogTrigger,
238  AnalogTriggerType triggerType);
239 
247  void SetDownSource(DigitalSource* source);
248 
257  void SetDownSource(DigitalSource& source);
258 
259  void SetDownSource(std::shared_ptr<DigitalSource> source);
260 
269  void SetDownSourceEdge(bool risingEdge, bool fallingEdge);
270 
274  void ClearDownSource();
275 
281  void SetUpDownCounterMode();
282 
290 
296  void SetSemiPeriodMode(bool highSemiPeriod);
297 
307  void SetPulseLengthMode(double threshold);
308 
318  void SetReverseDirection(bool reverseDirection);
319 
327  void SetSamplesToAverage(int samplesToAverage);
328 
338  int GetSamplesToAverage() const;
339 
340  int GetFPGAIndex() const;
341 
342  // CounterBase interface
349  int Get() const override;
350 
357  void Reset() override;
358 
367  double GetPeriod() const override;
368 
379  void SetMaxPeriod(double maxPeriod) override;
380 
398  void SetUpdateWhenEmpty(bool enabled);
399 
410  bool GetStopped() const override;
411 
417  bool GetDirection() const override;
418 
419  void InitSendable(SendableBuilder& builder) override;
420 
421  protected:
422  // Makes the counter count up.
423  std::shared_ptr<DigitalSource> m_upSource;
424 
425  // Makes the counter count down.
426  std::shared_ptr<DigitalSource> m_downSource;
427 
428  // The FPGA counter object
429  HAL_CounterHandle m_counter = HAL_kInvalidHandle;
430 
431  private:
432  int m_index = 0; // The index of this counter.
433 
434  friend class DigitalGlitchFilter;
435 };
436 
437 } // 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:34
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.