2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Encoder.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2018-10-29 12:49:17 -07:00
|
|
|
#include <hal/Encoder.h>
|
|
|
|
|
#include <hal/FRCUsageReporting.h>
|
2021-05-26 07:24:53 -07:00
|
|
|
#include <wpi/NullDeleter.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
#include <wpi/sendable/SendableRegistry.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/DigitalInput.h"
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
Encoder::Encoder(int aChannel, int bChannel, bool reverseDirection,
|
2015-06-24 01:06:29 -07:00
|
|
|
EncodingType encodingType) {
|
2015-07-29 16:48:04 -04:00
|
|
|
m_aSource = std::make_shared<DigitalInput>(aChannel);
|
|
|
|
|
m_bSource = std::make_shared<DigitalInput>(bChannel);
|
2015-06-25 15:07:55 -04:00
|
|
|
InitEncoder(reverseDirection, encodingType);
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddChild(this, m_aSource.get());
|
|
|
|
|
wpi::SendableRegistry::AddChild(this, m_bSource.get());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
Encoder::Encoder(DigitalSource* aSource, DigitalSource* bSource,
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
bool reverseDirection, EncodingType encodingType)
|
2021-05-26 07:24:53 -07:00
|
|
|
: m_aSource(aSource, wpi::NullDeleter<DigitalSource>()),
|
|
|
|
|
m_bSource(bSource, wpi::NullDeleter<DigitalSource>()) {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!m_aSource) {
|
2022-10-19 10:49:27 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "aSource");
|
2021-04-18 20:35:29 -07:00
|
|
|
}
|
|
|
|
|
if (!m_bSource) {
|
2022-10-19 10:49:27 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "bSource");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2021-04-18 20:35:29 -07:00
|
|
|
InitEncoder(reverseDirection, encodingType);
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
Encoder::Encoder(DigitalSource& aSource, DigitalSource& bSource,
|
|
|
|
|
bool reverseDirection, EncodingType encodingType)
|
2021-05-26 07:24:53 -07:00
|
|
|
: m_aSource(&aSource, wpi::NullDeleter<DigitalSource>()),
|
|
|
|
|
m_bSource(&bSource, wpi::NullDeleter<DigitalSource>()) {
|
2018-05-31 20:47:15 -07:00
|
|
|
InitEncoder(reverseDirection, encodingType);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
Encoder::Encoder(std::shared_ptr<DigitalSource> aSource,
|
2016-05-20 17:30:37 -07:00
|
|
|
std::shared_ptr<DigitalSource> bSource, bool reverseDirection,
|
|
|
|
|
EncodingType encodingType)
|
2020-12-28 10:12:52 -08:00
|
|
|
: m_aSource(std::move(aSource)), m_bSource(std::move(bSource)) {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!m_aSource) {
|
2022-10-19 10:49:27 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "aSource");
|
2021-04-18 20:35:29 -07:00
|
|
|
}
|
|
|
|
|
if (!m_bSource) {
|
2022-10-19 10:49:27 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "bSource");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2021-04-18 20:35:29 -07:00
|
|
|
InitEncoder(reverseDirection, encodingType);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Encoder::~Encoder() {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_FreeEncoder(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_ReportError(status, "FreeEncoder");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int Encoder::Get() const {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
int value = HAL_GetEncoder(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Get");
|
2016-07-03 15:22:22 -07:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::Reset() {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ResetEncoder(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Reset");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
units::second_t Encoder::GetPeriod() const {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
double value = HAL_GetEncoderPeriod(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetPeriod");
|
2021-05-28 22:06:59 -07:00
|
|
|
return units::second_t{value};
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
void Encoder::SetMaxPeriod(units::second_t maxPeriod) {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2021-10-25 08:58:12 -07:00
|
|
|
HAL_SetEncoderMaxPeriod(m_encoder, maxPeriod.value(), &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetMaxPeriod");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Encoder::GetStopped() const {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
bool value = HAL_GetEncoderStopped(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetStopped");
|
2016-07-03 15:22:22 -07:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Encoder::GetDirection() const {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
bool value = HAL_GetEncoderDirection(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetDirection");
|
2016-07-03 15:22:22 -07:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
int Encoder::GetRaw() const {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2018-05-31 20:47:15 -07:00
|
|
|
int value = HAL_GetEncoderRaw(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetRaw");
|
2018-05-31 20:47:15 -07:00
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Encoder::GetEncodingScale() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
int val = HAL_GetEncoderEncodingScale(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetEncodingScale");
|
2016-07-03 15:22:22 -07:00
|
|
|
return val;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double Encoder::GetDistance() const {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-11-20 07:25:03 -08:00
|
|
|
double value = HAL_GetEncoderDistance(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetDistance");
|
2016-07-03 15:22:22 -07:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double Encoder::GetRate() const {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-11-20 07:25:03 -08:00
|
|
|
double value = HAL_GetEncoderRate(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetRate");
|
2016-07-03 15:22:22 -07:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
void Encoder::SetMinRate(double minRate) {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetEncoderMinRate(m_encoder, minRate, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetMinRate");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
void Encoder::SetDistancePerPulse(double distancePerPulse) {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetEncoderDistancePerPulse(m_encoder, distancePerPulse, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetDistancePerPulse");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
double Encoder::GetDistancePerPulse() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
double distancePerPulse = HAL_GetEncoderDistancePerPulse(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetDistancePerPulse");
|
2017-12-04 23:28:33 -08:00
|
|
|
return distancePerPulse;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::SetReverseDirection(bool reverseDirection) {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetEncoderReverseDirection(m_encoder, reverseDirection, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetReverseDirection");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::SetSamplesToAverage(int samplesToAverage) {
|
|
|
|
|
if (samplesToAverage < 1 || samplesToAverage > 127) {
|
2021-04-18 20:35:29 -07:00
|
|
|
throw FRC_MakeError(
|
|
|
|
|
err::ParameterOutOfRange,
|
2021-05-23 19:33:33 -07:00
|
|
|
"Average counter values must be between 1 and 127, got {}",
|
|
|
|
|
samplesToAverage);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetEncoderSamplesToAverage(m_encoder, samplesToAverage, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetSamplesToAverage");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
int Encoder::GetSamplesToAverage() const {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
int result = HAL_GetEncoderSamplesToAverage(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetSamplesToAverage");
|
2015-06-25 15:07:55 -04:00
|
|
|
return result;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void Encoder::SetIndexSource(int channel, Encoder::IndexingType type) {
|
2016-07-07 21:43:55 -07:00
|
|
|
// Force digital input if just given an index
|
2017-12-29 19:48:39 -08:00
|
|
|
m_indexSource = std::make_shared<DigitalInput>(channel);
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddChild(this, m_indexSource.get());
|
2017-07-01 01:05:33 -04:00
|
|
|
SetIndexSource(*m_indexSource.get(), type);
|
2015-01-06 16:39:24 -05:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
void Encoder::SetIndexSource(const DigitalSource& source,
|
2015-06-25 15:07:55 -04:00
|
|
|
Encoder::IndexingType type) {
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
int32_t status = 0;
|
2020-12-28 11:04:20 -08:00
|
|
|
HAL_SetEncoderIndexSource(m_encoder, source.GetPortHandleForRouting(),
|
|
|
|
|
static_cast<HAL_AnalogTriggerType>(
|
|
|
|
|
source.GetAnalogTriggerTypeForRouting()),
|
|
|
|
|
static_cast<HAL_EncoderIndexingType>(type),
|
|
|
|
|
&status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "SetIndexSource");
|
2016-07-03 15:22:22 -07:00
|
|
|
}
|
|
|
|
|
|
2019-10-04 22:56:24 -07:00
|
|
|
void Encoder::SetSimDevice(HAL_SimDeviceHandle device) {
|
|
|
|
|
HAL_SetEncoderSimDevice(m_encoder, device);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int Encoder::GetFPGAIndex() const {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
int val = HAL_GetEncoderFPGAIndex(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetFPGAIndex");
|
2016-07-03 15:22:22 -07:00
|
|
|
return val;
|
2015-01-06 16:39:24 -05:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void Encoder::InitSendable(wpi::SendableBuilder& builder) {
|
2016-07-03 15:22:22 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_EncoderEncodingType type = HAL_GetEncoderEncodingType(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "GetEncodingType");
|
2020-12-28 12:58:06 -08:00
|
|
|
if (type == HAL_EncoderEncodingType::HAL_Encoder_k4X) {
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSmartDashboardType("Quadrature Encoder");
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSmartDashboardType("Encoder");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2022-10-15 16:33:14 -07:00
|
|
|
"Speed", [=, this] { return GetRate(); }, nullptr);
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2022-10-15 16:33:14 -07:00
|
|
|
"Distance", [=, this] { return GetDistance(); }, nullptr);
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
2022-10-15 16:33:14 -07:00
|
|
|
"Distance per Tick", [=, this] { return GetDistancePerPulse(); },
|
|
|
|
|
nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_encoder = HAL_InitializeEncoder(
|
|
|
|
|
m_aSource->GetPortHandleForRouting(),
|
2020-12-28 11:04:20 -08:00
|
|
|
static_cast<HAL_AnalogTriggerType>(
|
|
|
|
|
m_aSource->GetAnalogTriggerTypeForRouting()),
|
2018-05-31 20:47:15 -07:00
|
|
|
m_bSource->GetPortHandleForRouting(),
|
2020-12-28 11:04:20 -08:00
|
|
|
static_cast<HAL_AnalogTriggerType>(
|
|
|
|
|
m_bSource->GetAnalogTriggerTypeForRouting()),
|
|
|
|
|
reverseDirection, static_cast<HAL_EncoderEncodingType>(encodingType),
|
|
|
|
|
&status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "InitEncoder");
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Encoder, GetFPGAIndex() + 1,
|
2018-05-31 20:47:15 -07:00
|
|
|
encodingType);
|
2021-06-15 23:06:03 -07:00
|
|
|
wpi::SendableRegistry::AddLW(this, "Encoder", m_aSource->GetChannel());
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double Encoder::DecodingScaleFactor() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
double val = HAL_GetEncoderDecodingScaleFactor(m_encoder, &status);
|
2022-10-19 10:49:27 -07:00
|
|
|
FRC_CheckErrorStatus(status, "DecodingScaleFactor");
|
2018-05-31 20:47:15 -07:00
|
|
|
return val;
|
|
|
|
|
}
|