WPILibC++ 2025.0.0-alpha-1-2-g5e745bc
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>
13
14namespace frc {
15class AnalogInput;
16
17/**
18 * Class for supporting continuous analog encoders, such as the US Digital MA3.
19 */
21 public wpi::SendableHelper<AnalogEncoder> {
22 public:
23 /**
24 * Construct a new AnalogEncoder attached to a specific AnalogIn channel.
25 *
26 * <p>This has a fullRange of 1 and an expectedZero of 0.
27 *
28 * @param channel the analog input channel to attach to
29 */
30 explicit AnalogEncoder(int channel);
31
32 /**
33 * Construct a new AnalogEncoder attached to a specific AnalogInput.
34 *
35 * <p>This has a fullRange of 1 and an expectedZero of 0.
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 * <p>This has a fullRange of 1 and an expectedZero of 0.
45 *
46 * @param analogInput the analog input to attach to
47 */
48 explicit AnalogEncoder(AnalogInput* analogInput);
49
50 /**
51 * Construct a new AnalogEncoder attached to a specific AnalogInput.
52 *
53 * <p>This has a fullRange of 1 and an expectedZero of 0.
54 *
55 * @param analogInput the analog input to attach to
56 */
57 explicit AnalogEncoder(std::shared_ptr<AnalogInput> analogInput);
58
59 /**
60 * Construct a new AnalogEncoder attached to a specific AnalogIn channel.
61 *
62 * @param channel the analog input channel to attach to
63 * @param fullRange the value to report at maximum travel
64 * @param expectedZero the reading where you would expect a 0 from get()
65 */
66 AnalogEncoder(int channel, double fullRange, double expectedZero);
67
68 /**
69 * Construct a new AnalogEncoder attached to a specific AnalogInput.
70 *
71 * @param analogInput the analog input to attach to
72 * @param fullRange the value to report at maximum travel
73 * @param expectedZero the reading where you would expect a 0 from get()
74 */
75 AnalogEncoder(AnalogInput& analogInput, double fullRange,
76 double expectedZero);
77
78 /**
79 * Construct a new AnalogEncoder attached to a specific AnalogInput.
80 *
81 * @param analogInput the analog input to attach to
82 * @param fullRange the value to report at maximum travel
83 * @param expectedZero the reading where you would expect a 0 from get()
84 */
85 AnalogEncoder(AnalogInput* analogInput, double fullRange,
86 double expectedZero);
87
88 /**
89 * Construct a new AnalogEncoder attached to a specific AnalogInput.
90 *
91 * @param analogInput the analog input to attach to
92 * @param fullRange the value to report at maximum travel
93 * @param expectedZero the reading where you would expect a 0 from get()
94 */
95 AnalogEncoder(std::shared_ptr<AnalogInput> analogInput, double fullRange,
96 double expectedZero);
97
98 ~AnalogEncoder() override;
99
102
103 /**
104 * Get the encoder value.
105 *
106 * @return the encoder value scaled by the full range input
107 */
108 double Get() const;
109
110 /**
111 * Set the encoder voltage percentage range. Analog sensors are not always
112 * fully stable at the end of their travel ranges. Shrinking this range down
113 * can help mitigate issues with that.
114 *
115 * @param min minimum voltage percentage (0-1 range)
116 * @param max maximum voltage percentage (0-1 range)
117 */
118 void SetVoltagePercentageRange(double min, double max);
119
120 /**
121 * Set if this encoder is inverted.
122 *
123 * @param inverted true to invert the encoder, false otherwise
124 */
125 void SetInverted(bool inverted);
126
127 /**
128 * Get the channel number.
129 *
130 * @return The channel number.
131 */
132 int GetChannel() const;
133
134 void InitSendable(wpi::SendableBuilder& builder) override;
135
136 private:
137 void Init(double fullRange, double expectedZero);
138 double MapSensorRange(double pos) const;
139
140 std::shared_ptr<AnalogInput> m_analogInput;
141 double m_fullRange;
142 double m_expectedZero;
143 double m_sensorMin{0.0};
144 double m_sensorMax{1.0};
145 bool m_isInverted{false};
146
147 hal::SimDevice m_simDevice;
148 hal::SimDouble m_simPosition;
149};
150} // namespace frc
Class for supporting continuous analog encoders, such as the US Digital MA3.
Definition: AnalogEncoder.h:21
AnalogEncoder(std::shared_ptr< AnalogInput > analogInput, double fullRange, double expectedZero)
Construct a new AnalogEncoder attached to a specific AnalogInput.
AnalogEncoder(AnalogInput *analogInput, double fullRange, double expectedZero)
Construct a new AnalogEncoder attached to a specific AnalogInput.
AnalogEncoder(AnalogInput &analogInput, double fullRange, double expectedZero)
Construct a new AnalogEncoder attached to a specific AnalogInput.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
AnalogEncoder & operator=(AnalogEncoder &&)=default
void SetInverted(bool inverted)
Set if this encoder is inverted.
AnalogEncoder(int channel, double fullRange, double expectedZero)
Construct a new AnalogEncoder attached to a specific AnalogIn channel.
AnalogEncoder(int channel)
Construct a new AnalogEncoder attached to a specific AnalogIn channel.
int GetChannel() const
Get the channel number.
AnalogEncoder(AnalogInput &analogInput)
Construct a new AnalogEncoder attached to a specific AnalogInput.
~AnalogEncoder() override
AnalogEncoder(AnalogInput *analogInput)
Construct a new AnalogEncoder attached to a specific AnalogInput.
AnalogEncoder(std::shared_ptr< AnalogInput > analogInput)
Construct a new AnalogEncoder attached to a specific AnalogInput.
AnalogEncoder(AnalogEncoder &&)=default
void SetVoltagePercentageRange(double min, double max)
Set the encoder voltage percentage range.
double Get() const
Get the encoder value.
Analog input class.
Definition: AnalogInput.h:31
A move-only C++ wrapper around a HAL simulator device handle.
Definition: SimDevice.h:645
C++ wrapper around a HAL simulator double value handle.
Definition: SimDevice.h:536
Helper class for building Sendable dashboard representations.
Definition: SendableBuilder.h:21
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: AprilTagDetector_cv.h:11
constexpr UnitTypeLhs() min(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3411
constexpr UnitTypeLhs() max(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3419