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