blob: db3c74c3cb6be826dd8fc8cdfcd62a25b5b90d52 [file] [log] [blame]
jerrymf1579332013-02-07 01:56:28 +00001/*----------------------------------------------------------------------------*/
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
7#ifndef QUAD_ENCODER_H_
8#define QUAD_ENCODER_H_
9
10#include "ChipObject.h"
11#include "CounterBase.h"
12#include "SensorBase.h"
13#include "Counter.h"
14#include "PIDSource.h"
15#include "LiveWindow/LiveWindowSendable.h"
16
17class DigitalSource;
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 */
28class Encoder: public SensorBase, public CounterBase, public PIDSource, public LiveWindowSendable
29{
30public:
31 typedef enum {kDistance, kRate} PIDSourceParameter;
32
33 Encoder(UINT32 aChannel, UINT32 bChannel, bool reverseDirection=false, EncodingType encodingType = k4X);
34 Encoder(UINT8 aModuleNumber, UINT32 aChannel, UINT8 bModuleNumber, UINT32 _bChannel, bool reverseDirection=false, EncodingType encodingType = k4X);
35 Encoder(DigitalSource *aSource, DigitalSource *bSource, bool reverseDirection=false, EncodingType encodingType = k4X);
36 Encoder(DigitalSource &aSource, DigitalSource &bSource, bool reverseDirection=false, EncodingType encodingType = k4X);
37 virtual ~Encoder();
38
39 // CounterBase interface
40 void Start();
41 INT32 Get();
42 INT32 GetRaw();
43 void Reset();
44 void Stop();
45 double GetPeriod();
46 void SetMaxPeriod(double maxPeriod);
47 bool GetStopped();
48 bool GetDirection();
49 double GetDistance();
50 double GetRate();
51 void SetMinRate(double minRate);
52 void SetDistancePerPulse(double distancePerPulse);
53 void SetReverseDirection(bool reverseDirection);
54
55 void SetPIDSourceParameter(PIDSourceParameter pidSource);
56 double PIDGet();
57
58 void UpdateTable();
59 void StartLiveWindowMode();
60 void StopLiveWindowMode();
61 std::string GetSmartDashboardType();
62 void InitTable(ITable *subTable);
63 ITable * GetTable();
64
65private:
66 void InitEncoder(bool _reverseDirection, EncodingType encodingType);
67 double DecodingScaleFactor();
68
69 DigitalSource *m_aSource; // the A phase of the quad encoder
70 DigitalSource *m_bSource; // the B phase of the quad encoder
71 bool m_allocatedASource; // was the A source allocated locally?
72 bool m_allocatedBSource; // was the B source allocated locally?
73 tEncoder* m_encoder;
74 UINT8 m_index;
75 double m_distancePerPulse; // distance of travel for each encoder tick
76 Counter *m_counter; // Counter object for 1x and 2x encoding
77 EncodingType m_encodingType; // Encoding type
78 PIDSourceParameter m_pidSource;// Encoder parameter that sources a PID controller
79
80 ITable *m_table;
81};
82
83#endif
84