WPILibC++  2020.3.2
AnalogEncoder.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019 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/SimDevice.h>
13 #include <hal/Types.h>
14 #include <units/units.h>
15 
16 #include "frc/AnalogTrigger.h"
17 #include "frc/Counter.h"
18 #include "frc/ErrorBase.h"
19 #include "frc/smartdashboard/Sendable.h"
20 #include "frc/smartdashboard/SendableHelper.h"
21 
22 namespace frc {
23 class AnalogInput;
24 
28 class AnalogEncoder : public ErrorBase,
29  public Sendable,
30  public SendableHelper<AnalogEncoder> {
31  public:
37  explicit AnalogEncoder(AnalogInput& analogInput);
38 
44  explicit AnalogEncoder(AnalogInput* analogInput);
45 
51  explicit AnalogEncoder(std::shared_ptr<AnalogInput> analogInput);
52 
53  ~AnalogEncoder() override = default;
54 
55  AnalogEncoder(AnalogEncoder&&) = default;
56  AnalogEncoder& operator=(AnalogEncoder&&) = default;
57 
61  void Reset();
62 
70  units::turn_t Get() const;
71 
81  double GetPositionOffset() const;
82 
93  void SetDistancePerRotation(double distancePerRotation);
94 
101  double GetDistancePerRotation() const;
102 
109  double GetDistance() const;
110 
111  void InitSendable(SendableBuilder& builder) override;
112 
113  private:
114  void Init();
115 
116  std::shared_ptr<AnalogInput> m_analogInput;
117  AnalogTrigger m_analogTrigger;
118  Counter m_counter;
119  double m_positionOffset = 0;
120  double m_distancePerRotation = 1.0;
121  mutable units::turn_t m_lastPosition{0.0};
122 
123  hal::SimDevice m_simDevice;
124  hal::SimDouble m_simPosition;
125 };
126 } // namespace frc
frc::AnalogTrigger
Definition: AnalogTrigger.h:25
frc::AnalogEncoder::InitSendable
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
frc::AnalogEncoder::Reset
void Reset()
Reset the Encoder distance to zero.
frc::AnalogEncoder::GetDistancePerRotation
double GetDistancePerRotation() const
Get the distance per rotation for this encoder.
frc::AnalogEncoder::SetDistancePerRotation
void SetDistancePerRotation(double distancePerRotation)
Set the distance per rotation of the encoder.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::AnalogEncoder
Class for supporting continuous analog encoders, such as the US Digital MA3.
Definition: AnalogEncoder.h:28
frc::AnalogEncoder::GetPositionOffset
double GetPositionOffset() const
Get the offset of position relative to the last reset.
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc::AnalogEncoder::GetDistance
double GetDistance() const
Get the distance the sensor has driven since the last reset as scaled by the value from SetDistancePe...
frc::AnalogInput
Analog input class.
Definition: AnalogInput.h:37
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::AnalogEncoder::AnalogEncoder
AnalogEncoder(AnalogInput &analogInput)
Construct a new AnalogEncoder attached to a specific AnalogInput.
frc::AnalogEncoder::Get
units::turn_t Get() const
Get the encoder value since the last reset.
frc::Counter
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:37
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::SendableBuilder
Definition: SendableBuilder.h:23