blob: 76bfaf99a17728c7a3bf31e8c9df0ebcb9840234 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7#pragma once
8
9#include "HAL/HAL.hpp"
10#include "CounterBase.h"
11#include "SensorBase.h"
12#include "Counter.h"
13#include "PIDSource.h"
14#include "LiveWindow/LiveWindowSendable.h"
15
16#include <memory>
17
18class DigitalSource;
19class DigitalGlitchFilter;
20
21/**
22 * Class to read quad encoders.
23 * Quadrature encoders are devices that count shaft rotation and can sense
24 * direction. The output of
25 * the QuadEncoder class is an integer that can count either up or down, and can
26 * go negative for
27 * reverse direction counting. When creating QuadEncoders, a direction is
28 * supplied that changes the
29 * sense of the output to make code more readable if the encoder is mounted such
30 * that forward movement
31 * generates negative values. Quadrature encoders have two digital outputs, an A
32 * Channel and a B Channel
33 * that are out of phase with each other to allow the FPGA to do direction
34 * sensing.
35 *
36 * All encoders will immediately start counting - Reset() them if you need them
37 * to be zeroed before use.
38 */
39class Encoder : public SensorBase,
40 public CounterBase,
41 public PIDSource,
42 public LiveWindowSendable {
43 public:
44 enum IndexingType {
45 kResetWhileHigh,
46 kResetWhileLow,
47 kResetOnFallingEdge,
48 kResetOnRisingEdge
49 };
50
51 Encoder(uint32_t aChannel, uint32_t bChannel, bool reverseDirection = false,
52 EncodingType encodingType = k4X);
53 Encoder(std::shared_ptr<DigitalSource> aSource,
54 std::shared_ptr<DigitalSource> bSource,
55 bool reverseDirection = false, EncodingType encodingType = k4X);
56 Encoder(DigitalSource *aSource, DigitalSource *bSource,
57 bool reverseDirection = false, EncodingType encodingType = k4X);
58 Encoder(DigitalSource &aSource, DigitalSource &bSource,
59 bool reverseDirection = false, EncodingType encodingType = k4X);
60 virtual ~Encoder();
61
62 // CounterBase interface
63 int32_t Get() const override;
64 int32_t GetRaw() const;
65 int32_t GetEncodingScale() const;
66 void Reset() override;
67 double GetPeriod() const override;
68 void SetMaxPeriod(double maxPeriod) override;
69 bool GetStopped() const override;
70 bool GetDirection() const override;
71
72 double GetDistance() const;
73 double GetRate() const;
74 void SetMinRate(double minRate);
75 void SetDistancePerPulse(double distancePerPulse);
76 void SetReverseDirection(bool reverseDirection);
77 void SetSamplesToAverage(int samplesToAverage);
78 int GetSamplesToAverage() const;
79 double PIDGet() override;
80
81 void SetIndexSource(uint32_t channel, IndexingType type = kResetOnRisingEdge);
82 DEPRECATED("Use pass-by-reference instead.")
83 void SetIndexSource(DigitalSource *source,
84 IndexingType type = kResetOnRisingEdge);
85 void SetIndexSource(const DigitalSource &source,
86 IndexingType type = kResetOnRisingEdge);
87
88 void UpdateTable() override;
89 void StartLiveWindowMode() override;
90 void StopLiveWindowMode() override;
91 std::string GetSmartDashboardType() const override;
92 void InitTable(std::shared_ptr<ITable> subTable) override;
93 std::shared_ptr<ITable> GetTable() const override;
94
95 int32_t GetFPGAIndex() const { return m_index; }
96
97 private:
98 void InitEncoder(bool _reverseDirection, EncodingType encodingType);
99 double DecodingScaleFactor() const;
100
101 std::shared_ptr<DigitalSource> m_aSource; // the A phase of the quad encoder
102 std::shared_ptr<DigitalSource> m_bSource; // the B phase of the quad encoder
103 void *m_encoder = nullptr;
104 int32_t m_index = 0; // The encoder's FPGA index.
105 double m_distancePerPulse = 1.0; // distance of travel for each encoder tick
106 std::unique_ptr<Counter> m_counter =
107 nullptr; // Counter object for 1x and 2x encoding
108 EncodingType m_encodingType; // Encoding type
109 int32_t m_encodingScale; // 1x, 2x, or 4x, per the encodingType
110
111 std::shared_ptr<ITable> m_table;
112 friend class DigitalGlitchFilter;
113};