WPILibC++ 2023.4.3-108-ge5452e3
AnalogEncoder.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <memory>
8
9#include <hal/SimDevice.h>
10#include <hal/Types.h>
11#include <units/angle.h>
14
15#include "frc/AnalogTrigger.h"
16#include "frc/Counter.h"
17
18namespace frc {
19class AnalogInput;
20
21/**
22 * Class for supporting continuous analog encoders, such as the US Digital MA3.
23 */
25 public wpi::SendableHelper<AnalogEncoder> {
26 public:
27 /**
28 * Construct a new AnalogEncoder attached to a specific AnalogIn channel.
29 *
30 * @param channel the analog input channel to attach to
31 */
32 explicit AnalogEncoder(int channel);
33
34 /**
35 * Construct a new AnalogEncoder attached to a specific AnalogInput.
36 *
37 * @param analogInput the analog input to attach to
38 */
39 explicit AnalogEncoder(AnalogInput& analogInput);
40
41 /**
42 * Construct a new AnalogEncoder attached to a specific AnalogInput.
43 *
44 * @param analogInput the analog input to attach to
45 */
46 explicit AnalogEncoder(AnalogInput* analogInput);
47
48 /**
49 * Construct a new AnalogEncoder attached to a specific AnalogInput.
50 *
51 * @param analogInput the analog input to attach to
52 */
53 explicit AnalogEncoder(std::shared_ptr<AnalogInput> analogInput);
54
55 ~AnalogEncoder() override = default;
56
59
60 /**
61 * Reset the Encoder distance to zero.
62 */
63 void Reset();
64
65 /**
66 * Get the encoder value since the last reset.
67 *
68 * This is reported in rotations since the last reset.
69 *
70 * @return the encoder value in rotations
71 */
72 units::turn_t Get() const;
73
74 /**
75 * Get the absolute position of the analog encoder.
76 *
77 * <p>GetAbsolutePosition() - GetPositionOffset() will give an encoder
78 * absolute position relative to the last reset. This could potentially be
79 * negative, which needs to be accounted for.
80 *
81 * <p>This will not account for rollovers, and will always be just the raw
82 * absolute position.
83 *
84 * @return the absolute position
85 */
86 double GetAbsolutePosition() const;
87
88 /**
89 * Get the offset of position relative to the last reset.
90 *
91 * GetAbsolutePosition() - GetPositionOffset() will give an encoder absolute
92 * position relative to the last reset. This could potentially be negative,
93 * which needs to be accounted for.
94 *
95 * @return the position offset
96 */
97 double GetPositionOffset() const;
98
99 /**
100 * Set the position offset.
101 *
102 * <p>This must be in the range of 0-1.
103 *
104 * @param offset the offset
105 */
106 void SetPositionOffset(double offset);
107
108 /**
109 * Set the distance per rotation of the encoder. This sets the multiplier used
110 * to determine the distance driven based on the rotation value from the
111 * encoder. Set this value based on the how far the mechanism travels in 1
112 * rotation of the encoder, and factor in gearing reductions following the
113 * encoder shaft. This distance can be in any units you like, linear or
114 * angular.
115 *
116 * @param distancePerRotation the distance per rotation of the encoder
117 */
118 void SetDistancePerRotation(double distancePerRotation);
119
120 /**
121 * Get the distance per rotation for this encoder.
122 *
123 * @return The scale factor that will be used to convert rotation to useful
124 * units.
125 */
127
128 /**
129 * Get the distance the sensor has driven since the last reset as scaled by
130 * the value from SetDistancePerRotation.
131 *
132 * @return The distance driven since the last reset
133 */
134 double GetDistance() const;
135
136 /**
137 * Get the channel number.
138 *
139 * @return The channel number.
140 */
141 int GetChannel() const;
142
143 void InitSendable(wpi::SendableBuilder& builder) override;
144
145 private:
146 void Init();
147
148 std::shared_ptr<AnalogInput> m_analogInput;
149 AnalogTrigger m_analogTrigger;
150 Counter m_counter;
151 double m_positionOffset = 0;
152 double m_distancePerRotation = 1.0;
153 mutable units::turn_t m_lastPosition{0.0};
154
155 hal::SimDevice m_simDevice;
156 hal::SimDouble m_simPosition;
157 hal::SimDouble m_simAbsolutePosition;
158};
159} // namespace frc
Class for supporting continuous analog encoders, such as the US Digital MA3.
Definition: AnalogEncoder.h:25
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
AnalogEncoder & operator=(AnalogEncoder &&)=default
double GetPositionOffset() const
Get the offset of position relative to the last reset.
double GetDistance() const
Get the distance the sensor has driven since the last reset as scaled by the value from SetDistancePe...
void SetDistancePerRotation(double distancePerRotation)
Set the distance per rotation of the encoder.
AnalogEncoder(int channel)
Construct a new AnalogEncoder attached to a specific AnalogIn channel.
double GetAbsolutePosition() const
Get the absolute position of the analog encoder.
int GetChannel() const
Get the channel number.
AnalogEncoder(AnalogInput &analogInput)
Construct a new AnalogEncoder attached to a specific AnalogInput.
AnalogEncoder(AnalogInput *analogInput)
Construct a new AnalogEncoder attached to a specific AnalogInput.
void SetPositionOffset(double offset)
Set the position offset.
~AnalogEncoder() override=default
AnalogEncoder(std::shared_ptr< AnalogInput > analogInput)
Construct a new AnalogEncoder attached to a specific AnalogInput.
units::turn_t Get() const
Get the encoder value since the last reset.
AnalogEncoder(AnalogEncoder &&)=default
void Reset()
Reset the Encoder distance to zero.
double GetDistancePerRotation() const
Get the distance per rotation for this encoder.
Analog input class.
Definition: AnalogInput.h:31
Definition: AnalogTrigger.h:21
Class for counting the number of ticks on a digital input channel.
Definition: Counter.h:35
A move-only C++ wrapper around a HAL simulator device handle.
Definition: SimDevice.h:644
C++ wrapper around a HAL simulator double value handle.
Definition: SimDevice.h:535
Definition: SendableBuilder.h:18
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:19
Interface for Sendable objects.
Definition: Sendable.h:16
Definition: AprilTagPoseEstimator.h:15