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