WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
DutyCycleEncoder.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
10#include "wpi/units/frequency.hpp"
11#include "wpi/units/time.hpp"
14
15namespace wpi {
16class DutyCycle;
17
18/**
19 * Class for supporting duty cycle/PWM encoders, such as the US Digital MA3 with
20 * PWM Output, the CTRE Mag Encoder, the Rev Hex Encoder, and the AM Mag
21 * Encoder.
22 */
24 public wpi::util::SendableHelper<DutyCycleEncoder> {
25 public:
26 /**
27 * Construct a new DutyCycleEncoder on a specific channel.
28 *
29 * <p>This has a fullRange of 1 and an expectedZero of 0.
30 *
31 * @param channel the channel to attach to
32 */
33 explicit DutyCycleEncoder(int channel);
34
35 /**
36 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
37 *
38 * <p>This has a fullRange of 1 and an expectedZero of 0.
39 *
40 * @param dutyCycle the duty cycle to attach to
41 */
42 explicit DutyCycleEncoder(DutyCycle& dutyCycle);
43
44 /**
45 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
46 *
47 * <p>This has a fullRange of 1 and an expectedZero of 0.
48 *
49 * @param dutyCycle the duty cycle to attach to
50 */
51 explicit DutyCycleEncoder(DutyCycle* dutyCycle);
52
53 /**
54 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
55 *
56 * <p>This has a fullRange of 1 and an expectedZero of 0.
57 *
58 * @param dutyCycle the duty cycle to attach to
59 */
60 explicit DutyCycleEncoder(std::shared_ptr<DutyCycle> dutyCycle);
61
62 /**
63 * Construct a new DutyCycleEncoder on a specific channel.
64 *
65 * @param channel the channel to attach to
66 * @param fullRange the value to report at maximum travel
67 * @param expectedZero the reading where you would expect a 0 from get()
68 */
69 DutyCycleEncoder(int channel, double fullRange, double expectedZero);
70
71 /**
72 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
73 *
74 * @param dutyCycle the duty cycle to attach to
75 * @param fullRange the value to report at maximum travel
76 * @param expectedZero the reading where you would expect a 0 from get()
77 */
78 DutyCycleEncoder(DutyCycle& dutyCycle, double fullRange, double expectedZero);
79
80 /**
81 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
82 *
83 * @param dutyCycle the duty cycle to attach to
84 * @param fullRange the value to report at maximum travel
85 * @param expectedZero the reading where you would expect a 0 from get()
86 */
87 DutyCycleEncoder(DutyCycle* dutyCycle, double fullRange, double expectedZero);
88
89 /**
90 * Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
91 *
92 * @param dutyCycle the duty cycle to attach to
93 * @param fullRange the value to report at maximum travel
94 * @param expectedZero the reading where you would expect a 0 from get()
95 */
96 DutyCycleEncoder(std::shared_ptr<DutyCycle> dutyCycle, double fullRange,
97 double expectedZero);
98
99 ~DutyCycleEncoder() override = default;
100
103
104 /**
105 * Get the frequency of the duty cycle signal from the encoder.
106 *
107 * @return duty cycle frequency
108 */
109 wpi::units::hertz_t GetFrequency() const;
110
111 /**
112 * Get if the sensor is connected
113 *
114 * This uses the duty cycle frequency to determine if the sensor is connected.
115 * By default, a value of 100 Hz is used as the threshold, and this value can
116 * be changed with SetConnectedFrequencyThreshold.
117 *
118 * @return true if the sensor is connected
119 */
120 bool IsConnected() const;
121
122 /**
123 * Change the frequency threshold for detecting connection used by
124 * IsConnected.
125 *
126 * @param frequency the minimum frequency.
127 */
128 void SetConnectedFrequencyThreshold(wpi::units::hertz_t frequency);
129
130 /**
131 * Get the encoder value.
132 *
133 * @return the encoder value scaled by the full range input
134 */
135 double Get() const;
136
137 /**
138 * Set the encoder duty cycle range. As the encoder needs to maintain a duty
139 * cycle, the duty cycle cannot go all the way to 0% or all the way to 100%.
140 * For example, an encoder with a 4096 us period might have a minimum duty
141 * cycle of 1 us / 4096 us and a maximum duty cycle of 4095 / 4096 us. Setting
142 * the range will result in an encoder duty cycle less than or equal to the
143 * minimum being output as 0 rotation, the duty cycle greater than or equal to
144 * the maximum being output as 1 rotation, and values in between linearly
145 * scaled from 0 to 1.
146 *
147 * @param min minimum duty cycle (0-1 range)
148 * @param max maximum duty cycle (0-1 range)
149 */
150 void SetDutyCycleRange(double min, double max);
151
152 /**
153 * Sets the assumed frequency of the connected device.
154 *
155 * <p>By default, the DutyCycle engine has to compute the frequency of the
156 * input signal. This can result in both delayed readings and jumpy readings.
157 * To solve this, you can pass the expected frequency of the sensor to this
158 * function. This will use that frequency to compute the DutyCycle percentage,
159 * rather than the computed frequency.
160 *
161 * @param frequency the assumed frequency of the sensor
162 */
163 void SetAssumedFrequency(wpi::units::hertz_t frequency);
164
165 /**
166 * Set if this encoder is inverted.
167 *
168 * @param inverted true to invert the encoder, false otherwise
169 */
170 void SetInverted(bool inverted);
171
172 /**
173 * Get the channel of the source.
174 *
175 * @return the source channel
176 */
177 int GetSourceChannel() const;
178
180
181 private:
182 void Init(double fullRange, double expectedZero);
183 double MapSensorRange(double pos) const;
184
185 std::shared_ptr<DutyCycle> m_dutyCycle;
186 wpi::units::hertz_t m_frequencyThreshold = {100_Hz};
187 double m_fullRange;
188 double m_expectedZero;
189 wpi::units::second_t m_period{0_s};
190 double m_sensorMin{0.0};
191 double m_sensorMax{1.0};
192 bool m_isInverted{false};
193
194 wpi::hal::SimDevice m_simDevice;
195 wpi::hal::SimDouble m_simPosition;
196 wpi::hal::SimBoolean m_simIsConnected;
197};
198} // namespace wpi
DutyCycleEncoder(DutyCycleEncoder &&)=default
DutyCycleEncoder(DutyCycle *dutyCycle, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
DutyCycleEncoder(int channel)
Construct a new DutyCycleEncoder on a specific channel.
void SetConnectedFrequencyThreshold(wpi::units::hertz_t frequency)
Change the frequency threshold for detecting connection used by IsConnected.
bool IsConnected() const
Get if the sensor is connected.
void InitSendable(wpi::util::SendableBuilder &builder) override
Initializes this Sendable object.
DutyCycleEncoder(std::shared_ptr< DutyCycle > dutyCycle)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
double Get() const
Get the encoder value.
DutyCycleEncoder & operator=(DutyCycleEncoder &&)=default
DutyCycleEncoder(int channel, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder on a specific channel.
DutyCycleEncoder(DutyCycle &dutyCycle, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
void SetAssumedFrequency(wpi::units::hertz_t frequency)
Sets the assumed frequency of the connected device.
~DutyCycleEncoder() override=default
int GetSourceChannel() const
Get the channel of the source.
void SetDutyCycleRange(double min, double max)
Set the encoder duty cycle range.
wpi::units::hertz_t GetFrequency() const
Get the frequency of the duty cycle signal from the encoder.
void SetInverted(bool inverted)
Set if this encoder is inverted.
DutyCycleEncoder(DutyCycle &dutyCycle)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
DutyCycleEncoder(DutyCycle *dutyCycle)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
DutyCycleEncoder(std::shared_ptr< DutyCycle > dutyCycle, double fullRange, double expectedZero)
Construct a new DutyCycleEncoder attached to an existing DutyCycle object.
Class to read a duty cycle PWM input.
Definition DutyCycle.hpp:23
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