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