blob: cef4bef331b1d601716abd3a9cf39f9b9bfe743f [file] [log] [blame]
Brian Silvermanf7f267a2017-02-04 16:16:08 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "Encoder.h"
9
10#include <sstream>
11
12#include "LiveWindow/LiveWindow.h"
13#include "WPIErrors.h"
14
15using namespace frc;
16
17/**
18 * Common initialization code for Encoders.
19 * This code allocates resources for Encoders and is common to all constructors.
20 *
21 * The counter will start counting immediately.
22 *
23 * @param reverseDirection If true, counts down instead of up (this is all
24 * relative)
25 * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
26 * decoding. If 4X is selected, then an encoder FPGA
27 * object is used and the returned counts will be 4x
28 * the encoder spec'd value since all rising and
29 * falling edges are counted. If 1X or 2X are selected
30 * then a counter object will be used and the returned
31 * value will either exactly match the spec'd count or
32 * be double (2x) the spec'd count.
33 */
34void Encoder::InitEncoder(int channelA, int channelB, bool reverseDirection,
35 EncodingType encodingType) {
36 m_table = nullptr;
37 this->channelA = channelA;
38 this->channelB = channelB;
39 m_encodingType = encodingType;
40 m_encodingScale = encodingType == k4X ? 4 : encodingType == k2X ? 2 : 1;
41
42 int index = 0;
43 m_distancePerPulse = 1.0;
44
45 LiveWindow::GetInstance()->AddSensor("Encoder", channelA, this);
46
47 if (channelB < channelA) { // Swap ports
48 int channel = channelB;
49 channelB = channelA;
50 channelA = channel;
51 m_reverseDirection = !reverseDirection;
52 } else {
53 m_reverseDirection = reverseDirection;
54 }
55 std::stringstream ss;
56 ss << "dio/" << channelA << "/" << channelB;
57 impl = new SimEncoder(ss.str());
58 impl->Start();
59}
60
61/**
62 * Encoder constructor.
63 *
64 * Construct a Encoder given a and b channels.
65 *
66 * The counter will start counting immediately.
67 *
68 * @param aChannel The a channel digital input channel.
69 * @param bChannel The b channel digital input channel.
70 * @param reverseDirection If true, counts down instead of up (this is all
71 * relative)
72 * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
73 * decoding. If 4X is selected, then an encoder FPGA
74 * object is used and the returned counts will be 4x
75 * the encoder spec'd value since all rising and
76 * falling edges are counted. If 1X or 2X are selected
77 * then a counter object will be used and the returned
78 * value will either exactly match the spec'd count or
79 * be double (2x) the spec'd count.
80 */
81Encoder::Encoder(int aChannel, int bChannel, bool reverseDirection,
82 EncodingType encodingType) {
83 InitEncoder(aChannel, bChannel, reverseDirection, encodingType);
84}
85
86/**
87 * Encoder constructor.
88 *
89 * Construct a Encoder given a and b channels as digital inputs. This is used in
90 * the case where the digital inputs are shared. The Encoder class will not
91 * allocate the digital inputs and assume that they already are counted.
92 *
93 * The counter will start counting immediately.
94 *
95 * @param aSource The source that should be used for the a channel.
96 * @param bSource the source that should be used for the b channel.
97 * @param reverseDirection If true, counts down instead of up (this is all
98 * relative)
99 * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
100 * decoding. If 4X is selected, then an encoder FPGA
101 * object is used and the returned counts will be 4x
102 * the encoder spec'd value since all rising and
103 * falling edges are counted. If 1X or 2X are selected
104 * then a counter object will be used and the returned
105 * value will either exactly match the spec'd count or
106 * be double (2x) the spec'd count.
107 */
108/* TODO: [Not Supported] Encoder::Encoder(DigitalSource *aSource, DigitalSource
109*bSource, bool reverseDirection, EncodingType encodingType) :
110 m_encoder(nullptr),
111 m_counter(nullptr)
112{
113 m_aSource = aSource;
114 m_bSource = bSource;
115 m_allocatedASource = false;
116 m_allocatedBSource = false;
117 if (m_aSource == nullptr || m_bSource == nullptr)
118 wpi_setWPIError(NullParameter);
119 else
120 InitEncoder(reverseDirection, encodingType);
121 }*/
122
123/**
124 * Encoder constructor.
125 *
126 * Construct a Encoder given a and b channels as digital inputs. This is used in
127 * the case where the digital inputs are shared. The Encoder class will not
128 * allocate the digital inputs and assume that they already are counted.
129 *
130 * The counter will start counting immediately.
131 *
132 * @param aSource The source that should be used for the a channel.
133 * @param bSource the source that should be used for the b channel.
134 * @param reverseDirection If true, counts down instead of up (this is all
135 * relative)
136 * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
137 * decoding. If 4X is selected, then an encoder FPGA
138 * object is used and the returned counts will be 4x
139 * the encoder spec'd value since all rising and
140 * falling edges are counted. If 1X or 2X are selected
141 * then a counter object will be used and the returned
142 * value will either exactly match the spec'd count or
143 * be double (2x) the spec'd count.
144 */
145/*// TODO: [Not Supported] Encoder::Encoder(DigitalSource &aSource,
146DigitalSource &bSource, bool reverseDirection, EncodingType encodingType) :
147 m_encoder(nullptr),
148 m_counter(nullptr)
149{
150 m_aSource = &aSource;
151 m_bSource = &bSource;
152 m_allocatedASource = false;
153 m_allocatedBSource = false;
154 InitEncoder(reverseDirection, encodingType);
155 }*/
156
157/**
158 * Reset the Encoder distance to zero.
159 *
160 * Resets the current count to zero on the encoder.
161 */
162void Encoder::Reset() { impl->Reset(); }
163
164/**
165 * Determine if the encoder is stopped.
166 *
167 * Using the MaxPeriod value, a boolean is returned that is true if the encoder
168 * is considered stopped and false if it is still moving. A stopped encoder is
169 * one where the most recent pulse width exceeds the MaxPeriod.
170 *
171 * @return True if the encoder is considered stopped.
172 */
173bool Encoder::GetStopped() const {
174 throw "Simulation doesn't currently support this method.";
175}
176
177/**
178 * The last direction the encoder value changed.
179 *
180 * @return The last direction the encoder value changed.
181 */
182bool Encoder::GetDirection() const {
183 throw "Simulation doesn't currently support this method.";
184}
185
186/**
187 * The scale needed to convert a raw counter value into a number of encoder
188 * pulses.
189 */
190double Encoder::DecodingScaleFactor() const {
191 switch (m_encodingType) {
192 case k1X:
193 return 1.0;
194 case k2X:
195 return 0.5;
196 case k4X:
197 return 0.25;
198 default:
199 return 0.0;
200 }
201}
202
203/**
204 * The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.
205 *
206 * Used to divide raw edge counts down to spec'd counts.
207 */
208int Encoder::GetEncodingScale() const { return m_encodingScale; }
209
210/**
211 * Gets the raw value from the encoder.
212 *
213 * The raw value is the actual count unscaled by the 1x, 2x, or 4x scale
214 * factor.
215 *
216 * @return Current raw count from the encoder
217 */
218int Encoder::GetRaw() const {
219 throw "Simulation doesn't currently support this method.";
220}
221
222/**
223 * Gets the current count.
224 *
225 * Returns the current count on the Encoder.
226 * This method compensates for the decoding type.
227 *
228 * @return Current count from the Encoder adjusted for the 1x, 2x, or 4x scale
229 * factor.
230 */
231int Encoder::Get() const {
232 throw "Simulation doesn't currently support this method.";
233}
234
235/**
236 * Returns the period of the most recent pulse.
237 *
238 * Returns the period of the most recent Encoder pulse in seconds.
239 * This method compenstates for the decoding type.
240 *
241 * @deprecated Use GetRate() in favor of this method. This returns unscaled
242 * periods and GetRate() scales using value from
243 * SetDistancePerPulse().
244 *
245 * @return Period in seconds of the most recent pulse.
246 */
247double Encoder::GetPeriod() const {
248 throw "Simulation doesn't currently support this method.";
249}
250
251/**
252 * Sets the maximum period for stopped detection.
253 *
254 * Sets the value that represents the maximum period of the Encoder before it
255 * will assume that the attached device is stopped. This timeout allows users
256 * to determine if the wheels or other shaft has stopped rotating.
257 * This method compensates for the decoding type.
258 *
259 * @deprecated Use SetMinRate() in favor of this method. This takes unscaled
260 * periods and SetMinRate() scales using value from
261 * SetDistancePerPulse().
262 *
263 * @param maxPeriod The maximum time between rising and falling edges before the
264 * FPGA will report the device stopped. This is expressed in
265 * seconds.
266 */
267void Encoder::SetMaxPeriod(double maxPeriod) {
268 throw "Simulation doesn't currently support this method.";
269}
270
271/**
272 * Get the distance the robot has driven since the last reset.
273 *
274 * @return The distance driven since the last reset as scaled by the value from
275 * SetDistancePerPulse().
276 */
277double Encoder::GetDistance() const {
278 return m_distancePerPulse * impl->GetPosition();
279}
280
281/**
282 * Get the current rate of the encoder.
283 *
284 * Units are distance per second as scaled by the value from
285 * SetDistancePerPulse().
286 *
287 * @return The current rate of the encoder.
288 */
289double Encoder::GetRate() const {
290 return m_distancePerPulse * impl->GetVelocity();
291}
292
293/**
294 * Set the minimum rate of the device before the hardware reports it stopped.
295 *
296 * @param minRate The minimum rate. The units are in distance per second as
297 * scaled by the value from SetDistancePerPulse().
298 */
299void Encoder::SetMinRate(double minRate) {
300 throw "Simulation doesn't currently support this method.";
301}
302
303/**
304 * Set the distance per pulse for this encoder.
305 *
306 * This sets the multiplier used to determine the distance driven based on the
307 * count value from the encoder. Do not include the decoding type in this scale.
308 * The library already compensates for the decoding type. Set this value based
309 * on the encoder's rated Pulses per Revolution and factor in gearing reductions
310 * following the encoder shaft. This distance can be in any units you like,
311 * linear or angular.
312 *
313 * @param distancePerPulse The scale factor that will be used to convert pulses
314 * to useful units.
315 */
316void Encoder::SetDistancePerPulse(double distancePerPulse) {
317 if (m_reverseDirection) {
318 m_distancePerPulse = -distancePerPulse;
319 } else {
320 m_distancePerPulse = distancePerPulse;
321 }
322}
323
324/**
325 * Set the direction sensing for this encoder.
326 *
327 * This sets the direction sensing on the encoder so that it could count in the
328 * correct software direction regardless of the mounting.
329 *
330 * @param reverseDirection true if the encoder direction should be reversed
331 */
332void Encoder::SetReverseDirection(bool reverseDirection) {
333 throw "Simulation doesn't currently support this method.";
334}
335
336/**
337 * Set which parameter of the encoder you are using as a process control
338 * variable.
339 *
340 * @param pidSource An enum to select the parameter.
341 */
342void Encoder::SetPIDSourceType(PIDSourceType pidSource) {
343 m_pidSource = pidSource;
344}
345
346/**
347 * Implement the PIDSource interface.
348 *
349 * @return The current value of the selected source parameter.
350 */
351double Encoder::PIDGet() {
352 switch (m_pidSource) {
353 case PIDSourceType::kDisplacement:
354 return GetDistance();
355 case PIDSourceType::kRate:
356 return GetRate();
357 default:
358 return 0.0;
359 }
360}
361
362void Encoder::UpdateTable() {
363 if (m_table != nullptr) {
364 m_table->PutNumber("Speed", GetRate());
365 m_table->PutNumber("Distance", GetDistance());
366 m_table->PutNumber("Distance per Tick", m_reverseDirection
367 ? -m_distancePerPulse
368 : m_distancePerPulse);
369 }
370}
371
372void Encoder::StartLiveWindowMode() {}
373
374void Encoder::StopLiveWindowMode() {}
375
376std::string Encoder::GetSmartDashboardType() const {
377 if (m_encodingType == k4X)
378 return "Quadrature Encoder";
379 else
380 return "Encoder";
381}
382
383void Encoder::InitTable(std::shared_ptr<ITable> subTable) {
384 m_table = subTable;
385 UpdateTable();
386}
387
388std::shared_ptr<ITable> Encoder::GetTable() const { return m_table; }