blob: 89b1b3a3037188dff3847c497788c017ac11fd43 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5/*----------------------------------------------------------------------------*/
6#pragma once
7
8#include "simulation/SimEncoder.h"
9#include "CounterBase.h"
10#include "SensorBase.h"
11#include "Counter.h"
12#include "PIDSource.h"
13#include "LiveWindow/LiveWindowSendable.h"
14
15#include <memory>
16
17/**
18 * Class to read quad encoders.
19 * Quadrature encoders are devices that count shaft rotation and can sense direction. The output of
20 * the QuadEncoder class is an integer that can count either up or down, and can go negative for
21 * reverse direction counting. When creating QuadEncoders, a direction is supplied that changes the
22 * sense of the output to make code more readable if the encoder is mounted such that forward movement
23 * generates negative values. Quadrature encoders have two digital outputs, an A Channel and a B Channel
24 * that are out of phase with each other to allow the FPGA to do direction sensing.
25 *
26 * All encoders will immediately start counting - Reset() them if you need them
27 * to be zeroed before use.
28 */
29class Encoder : public SensorBase, public CounterBase, public PIDSource, public LiveWindowSendable
30{
31public:
32
33 Encoder(uint32_t aChannel, uint32_t bChannel, bool reverseDirection = false,
34 EncodingType encodingType = k4X);
35 // TODO: [Not Supported] Encoder(DigitalSource *aSource, DigitalSource *bSource, bool reverseDirection=false, EncodingType encodingType = k4X);
36 // TODO: [Not Supported] Encoder(DigitalSource &aSource, DigitalSource &bSource, bool reverseDirection=false, EncodingType encodingType = k4X);
37 virtual ~Encoder() = default;
38
39 // CounterBase interface
40 int32_t Get() const override;
41 int32_t GetRaw() const;
42 int32_t GetEncodingScale() const;
43 void Reset() override;
44 double GetPeriod() const override;
45 void SetMaxPeriod(double maxPeriod) override;
46 bool GetStopped() const override;
47 bool GetDirection() const override;
48
49 double GetDistance() const;
50 double GetRate() const;
51 void SetMinRate(double minRate);
52 void SetDistancePerPulse(double distancePerPulse);
53 void SetReverseDirection(bool reverseDirection);
54 void SetSamplesToAverage(int samplesToAverage);
55 int GetSamplesToAverage() const;
56 void SetPIDSourceType(PIDSourceType pidSource);
57 double PIDGet() override;
58
59 void UpdateTable() override;
60 void StartLiveWindowMode() override;
61 void StopLiveWindowMode() override;
62 std::string GetSmartDashboardType() const override;
63 void InitTable(std::shared_ptr<ITable> subTable) override;
64 std::shared_ptr<ITable> GetTable() const override;
65
66 int32_t FPGAEncoderIndex() const
67 {
68 return 0;
69 }
70
71private:
72 void InitEncoder(int channelA, int channelB, bool _reverseDirection, EncodingType encodingType);
73 double DecodingScaleFactor() const;
74
75 // TODO: [Not Supported] DigitalSource *m_aSource; // the A phase of the quad encoder
76 // TODO: [Not Supported] DigitalSource *m_bSource; // the B phase of the quad encoder
77 // TODO: [Not Supported] bool m_allocatedASource; // was the A source allocated locally?
78 // TODO: [Not Supported] bool m_allocatedBSource; // was the B source allocated locally?
79 int channelA, channelB;
80 double m_distancePerPulse; // distance of travel for each encoder tick
81 EncodingType m_encodingType; // Encoding type
82 int32_t m_encodingScale; // 1x, 2x, or 4x, per the encodingType
83 bool m_reverseDirection;
84 SimEncoder* impl;
85
86 std::shared_ptr<ITable> m_table;
87};