2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Encoder.h"
|
|
|
|
|
#include "DigitalInput.h"
|
2014-01-06 10:12:21 -05:00
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Resource.h"
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Common initialization code for Encoders.
|
|
|
|
|
* This code allocates resources for Encoders and is common to all constructors.
|
2014-07-29 09:58:15 -07:00
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param reverseDirection If true, counts down instead of up (this is all
|
|
|
|
|
* relative)
|
|
|
|
|
* @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
|
|
|
|
|
* decoding. If 4X is
|
|
|
|
|
* selected, then an encoder FPGA object is used and the returned counts will be
|
|
|
|
|
* 4x the encoder
|
|
|
|
|
* spec'd value since all rising and falling edges are counted. If 1X or 2X are
|
|
|
|
|
* selected then
|
|
|
|
|
* a counter object will be used and the returned value will either exactly
|
|
|
|
|
* match the spec'd count
|
2013-12-15 18:30:16 -05:00
|
|
|
* or be double (2x) the spec'd count.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) {
|
|
|
|
|
m_encodingType = encodingType;
|
|
|
|
|
switch (encodingType) {
|
|
|
|
|
case k4X: {
|
|
|
|
|
m_encodingScale = 4;
|
|
|
|
|
if (m_aSource->StatusIsFatal()) {
|
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
|
|
|
CloneError(*m_aSource);
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (m_bSource->StatusIsFatal()) {
|
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
|
|
|
CloneError(*m_bSource);
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_encoder = initializeEncoder(
|
|
|
|
|
m_aSource->GetModuleForRouting(), m_aSource->GetChannelForRouting(),
|
|
|
|
|
m_aSource->GetAnalogTriggerForRouting(),
|
|
|
|
|
m_bSource->GetModuleForRouting(), m_bSource->GetChannelForRouting(),
|
|
|
|
|
m_bSource->GetAnalogTriggerForRouting(), reverseDirection, &m_index,
|
|
|
|
|
&status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2015-06-23 04:49:51 -07:00
|
|
|
m_counter = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
SetMaxPeriod(.5);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case k1X:
|
|
|
|
|
case k2X: {
|
|
|
|
|
m_encodingScale = encodingType == k1X ? 1 : 2;
|
2015-07-29 16:48:04 -04:00
|
|
|
m_counter = std::make_unique<Counter>(m_encodingType, m_aSource,
|
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
|
|
|
m_bSource, reverseDirection);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_index = m_counter->GetFPGAIndex();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
wpi_setErrorWithContext(-1, "Invalid encodingType argument");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Encoder, m_index, 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
|
|
|
LiveWindow::GetInstance().AddSensor("Encoder",
|
2015-06-25 15:07:55 -04:00
|
|
|
m_aSource->GetChannelForRouting(), this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encoder constructor.
|
2014-06-13 17:45:10 -04:00
|
|
|
* Construct a Encoder given a and b channels.
|
2014-07-29 09:58:15 -07:00
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param aChannel The a channel DIO channel. 0-9 are on-board, 10-25 are on the
|
|
|
|
|
* MXP port
|
|
|
|
|
* @param bChannel The b channel DIO channel. 0-9 are on-board, 10-25 are on the
|
|
|
|
|
* MXP port
|
|
|
|
|
* @param reverseDirection represents the orientation of the encoder and inverts
|
|
|
|
|
* the output values
|
2013-12-15 18:30:16 -05:00
|
|
|
* if necessary so forward represents positive values.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
|
|
|
|
|
* decoding. If 4X is
|
|
|
|
|
* selected, then an encoder FPGA object is used and the returned counts will be
|
|
|
|
|
* 4x the encoder
|
|
|
|
|
* spec'd value since all rising and falling edges are counted. If 1X or 2X are
|
|
|
|
|
* selected then
|
|
|
|
|
* a counter object will be used and the returned value will either exactly
|
|
|
|
|
* match the spec'd count
|
2013-12-15 18:30:16 -05:00
|
|
|
* or be double (2x) the spec'd count.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Encoder::Encoder(uint32_t aChannel, uint32_t 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);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encoder constructor.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Construct a Encoder given a and b channels as digital inputs. This is used in
|
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
|
|
|
* the case where the digital inputs are shared. The Encoder class will not
|
|
|
|
|
* allocate the digital inputs and assume that they already are counted.
|
2014-07-29 09:58:15 -07:00
|
|
|
* The counter will start counting immediately.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param aSource The source that should be used for the a channel.
|
|
|
|
|
* @param bSource the source that should be used for the b channel.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param reverseDirection represents the orientation of the encoder and inverts
|
|
|
|
|
* the output values
|
2013-12-15 18:30:16 -05:00
|
|
|
* if necessary so forward represents positive values.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
|
|
|
|
|
* decoding. If 4X is
|
|
|
|
|
* selected, then an encoder FPGA object is used and the returned counts will be
|
|
|
|
|
* 4x the encoder
|
|
|
|
|
* spec'd value since all rising and falling edges are counted. If 1X or 2X are
|
|
|
|
|
* selected then
|
|
|
|
|
* a counter object will be used and the returned value will either exactly
|
|
|
|
|
* match the spec'd count
|
2013-12-15 18:30:16 -05:00
|
|
|
* or be double (2x) the spec'd count.
|
|
|
|
|
*/
|
2015-06-30 15:01:20 -04:00
|
|
|
[[deprecated(
|
|
|
|
|
"Raw pointers are deprecated; if you wish to construct your own copy of "
|
|
|
|
|
"the DigitalSource and pass it to the constructor, use an std::shared_ptr "
|
|
|
|
|
"instead.")]]
|
2015-06-25 15:07:55 -04: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)
|
|
|
|
|
: m_aSource(aSource, NullDeleter<DigitalSource>()),
|
|
|
|
|
m_bSource(bSource, NullDeleter<DigitalSource>()) {
|
|
|
|
|
if (m_aSource == nullptr || m_bSource == nullptr)
|
|
|
|
|
wpi_setWPIError(NullParameter);
|
|
|
|
|
else
|
|
|
|
|
InitEncoder(reverseDirection, encodingType);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
Encoder::Encoder(std::shared_ptr<DigitalSource> aSource,
|
|
|
|
|
std::shared_ptr<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)
|
|
|
|
|
: m_aSource(aSource), m_bSource(bSource) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_aSource == nullptr || m_bSource == nullptr)
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIError(NullParameter);
|
|
|
|
|
else
|
|
|
|
|
InitEncoder(reverseDirection, encodingType);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encoder constructor.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Construct a Encoder given a and b channels as digital inputs. This is used in
|
|
|
|
|
* the case
|
|
|
|
|
* where the digital inputs are shared. The Encoder class will not allocate the
|
|
|
|
|
* digital inputs
|
2013-12-15 18:30:16 -05:00
|
|
|
* and assume that they already are counted.
|
2014-07-29 09:58:15 -07:00
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param aSource The source that should be used for the a channel.
|
|
|
|
|
* @param bSource the source that should be used for the b channel.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param reverseDirection represents the orientation of the encoder and inverts
|
|
|
|
|
* the output values
|
2013-12-15 18:30:16 -05:00
|
|
|
* if necessary so forward represents positive values.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
|
|
|
|
|
* decoding. If 4X is
|
|
|
|
|
* selected, then an encoder FPGA object is used and the returned counts will be
|
|
|
|
|
* 4x the encoder
|
|
|
|
|
* spec'd value since all rising and falling edges are counted. If 1X or 2X are
|
|
|
|
|
* selected then
|
|
|
|
|
* a counter object will be used and the returned value will either exactly
|
|
|
|
|
* match the spec'd count
|
2013-12-15 18:30:16 -05:00
|
|
|
* or be double (2x) the spec'd count.
|
|
|
|
|
*/
|
2015-06-30 15:01:20 -04:00
|
|
|
[[deprecated(
|
|
|
|
|
"References are deprecated; if you wish to construct your own copy of "
|
|
|
|
|
"the DigitalSource and pass it to the constructor, use an std::shared_ptr "
|
|
|
|
|
"instead.")]]
|
2015-06-25 15:07:55 -04: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)
|
|
|
|
|
: m_aSource(&aSource, NullDeleter<DigitalSource>()),
|
|
|
|
|
m_bSource(&bSource, NullDeleter<DigitalSource>())
|
|
|
|
|
{
|
2015-06-25 15:07:55 -04:00
|
|
|
InitEncoder(reverseDirection, encodingType);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Free the resources for an Encoder.
|
|
|
|
|
* Frees the FPGA resources associated with an Encoder.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Encoder::~Encoder() {
|
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
|
|
|
if (!m_counter) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
freeEncoder(m_encoder, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-30 17:36:12 -05:00
|
|
|
/**
|
|
|
|
|
* The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.
|
|
|
|
|
* Used to divide raw edge counts down to spec'd counts.
|
|
|
|
|
*/
|
2015-06-19 17:23:54 -07:00
|
|
|
int32_t Encoder::GetEncodingScale() const { return m_encodingScale; }
|
2014-12-30 17:36:12 -05:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Gets the raw value from the encoder.
|
|
|
|
|
* The raw value is the actual count unscaled by the 1x, 2x, or 4x scale
|
|
|
|
|
* factor.
|
|
|
|
|
* @return Current raw count from the encoder
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t Encoder::GetRaw() const {
|
|
|
|
|
if (StatusIsFatal()) return 0;
|
|
|
|
|
int32_t value;
|
|
|
|
|
if (m_counter)
|
|
|
|
|
value = m_counter->Get();
|
|
|
|
|
else {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
value = getEncoder(m_encoder, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
|
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the current count.
|
|
|
|
|
* Returns the current count on the Encoder.
|
|
|
|
|
* This method compensates for the decoding type.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return Current count from the Encoder adjusted for the 1x, 2x, or 4x scale
|
|
|
|
|
* factor.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t Encoder::Get() const {
|
|
|
|
|
if (StatusIsFatal()) return 0;
|
|
|
|
|
return (int32_t)(GetRaw() * DecodingScaleFactor());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the Encoder distance to zero.
|
|
|
|
|
* Resets the current count to zero on the encoder.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::Reset() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
if (m_counter)
|
|
|
|
|
m_counter->Reset();
|
|
|
|
|
else {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
resetEncoder(m_encoder, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the period of the most recent pulse.
|
|
|
|
|
* Returns the period of the most recent Encoder pulse in seconds.
|
2014-12-30 17:36:12 -05:00
|
|
|
* This method compensates for the decoding type.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @deprecated Use GetRate() in favor of this method. This returns unscaled
|
|
|
|
|
* periods and GetRate() scales using value from SetDistancePerPulse().
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
|
|
|
|
* @return Period in seconds of the most recent pulse.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Encoder::GetPeriod() const {
|
|
|
|
|
if (StatusIsFatal()) return 0.0;
|
|
|
|
|
if (m_counter) {
|
|
|
|
|
return m_counter->GetPeriod() / DecodingScaleFactor();
|
|
|
|
|
} else {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
double period = getEncoderPeriod(m_encoder, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return period;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the maximum period for stopped detection.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Sets the value that represents the maximum period of the Encoder before it
|
|
|
|
|
* will assume
|
|
|
|
|
* that the attached device is stopped. This timeout allows users to determine
|
|
|
|
|
* if the wheels or
|
2013-12-15 18:30:16 -05:00
|
|
|
* other shaft has stopped rotating.
|
|
|
|
|
* This method compensates for the decoding type.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @deprecated Use SetMinRate() in favor of this method. This takes unscaled
|
|
|
|
|
* periods and SetMinRate() scales using value from SetDistancePerPulse().
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param maxPeriod The maximum time between rising and falling edges before the
|
|
|
|
|
* FPGA will
|
2013-12-15 18:30:16 -05:00
|
|
|
* report the device stopped. This is expressed in seconds.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::SetMaxPeriod(double maxPeriod) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
if (m_counter) {
|
|
|
|
|
m_counter->SetMaxPeriod(maxPeriod * DecodingScaleFactor());
|
|
|
|
|
} else {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setEncoderMaxPeriod(m_encoder, maxPeriod, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the encoder is stopped.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Using the MaxPeriod value, a boolean is returned that is true if the encoder
|
|
|
|
|
* is considered
|
|
|
|
|
* stopped and false if it is still moving. A stopped encoder is one where the
|
|
|
|
|
* most recent pulse
|
2013-12-15 18:30:16 -05:00
|
|
|
* width exceeds the MaxPeriod.
|
|
|
|
|
* @return True if the encoder is considered stopped.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Encoder::GetStopped() const {
|
|
|
|
|
if (StatusIsFatal()) return true;
|
|
|
|
|
if (m_counter) {
|
|
|
|
|
return m_counter->GetStopped();
|
|
|
|
|
} else {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
bool value = getEncoderStopped(m_encoder, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The last direction the encoder value changed.
|
|
|
|
|
* @return The last direction the encoder value changed.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Encoder::GetDirection() const {
|
|
|
|
|
if (StatusIsFatal()) return false;
|
|
|
|
|
if (m_counter) {
|
|
|
|
|
return m_counter->GetDirection();
|
|
|
|
|
} else {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
bool value = getEncoderDirection(m_encoder, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* The scale needed to convert a raw counter value into a number of encoder
|
|
|
|
|
* pulses.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Encoder::DecodingScaleFactor() const {
|
|
|
|
|
if (StatusIsFatal()) return 0.0;
|
|
|
|
|
switch (m_encodingType) {
|
|
|
|
|
case k1X:
|
|
|
|
|
return 1.0;
|
|
|
|
|
case k2X:
|
|
|
|
|
return 0.5;
|
|
|
|
|
case k4X:
|
|
|
|
|
return 0.25;
|
|
|
|
|
default:
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the distance the robot has driven since the last reset.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return The distance driven since the last reset as scaled by the value from
|
|
|
|
|
* SetDistancePerPulse().
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Encoder::GetDistance() const {
|
|
|
|
|
if (StatusIsFatal()) return 0.0;
|
|
|
|
|
return GetRaw() * DecodingScaleFactor() * m_distancePerPulse;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current rate of the encoder.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Units are distance per second as scaled by the value from
|
|
|
|
|
* SetDistancePerPulse().
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The current rate of the encoder.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Encoder::GetRate() const {
|
|
|
|
|
if (StatusIsFatal()) return 0.0;
|
|
|
|
|
return (m_distancePerPulse / GetPeriod());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the minimum rate of the device before the hardware reports it stopped.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param minRate The minimum rate. The units are in distance per second as
|
|
|
|
|
* scaled by the value from SetDistancePerPulse().
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::SetMinRate(double minRate) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
SetMaxPeriod(m_distancePerPulse / minRate);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the distance per pulse for this encoder.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This sets the multiplier used to determine the distance driven based on the
|
|
|
|
|
* count value
|
2013-12-15 18:30:16 -05:00
|
|
|
* from the encoder.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Do not include the decoding type in this scale. The library already
|
|
|
|
|
* compensates for the decoding type.
|
2013-12-15 18:30:16 -05:00
|
|
|
* Set this value based on the encoder's rated Pulses per Revolution and
|
|
|
|
|
* factor in gearing reductions following the encoder shaft.
|
|
|
|
|
* This distance can be in any units you like, linear or angular.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param distancePerPulse The scale factor that will be used to convert pulses
|
|
|
|
|
* to useful units.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::SetDistancePerPulse(double distancePerPulse) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
m_distancePerPulse = distancePerPulse;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the direction sensing for this encoder.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This sets the direction sensing on the encoder so that it could count in the
|
|
|
|
|
* correct
|
2013-12-15 18:30:16 -05:00
|
|
|
* software direction regardless of the mounting.
|
|
|
|
|
* @param reverseDirection true if the encoder direction should be reversed
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::SetReverseDirection(bool reverseDirection) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
if (m_counter) {
|
|
|
|
|
m_counter->SetReverseDirection(reverseDirection);
|
|
|
|
|
} else {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setEncoderReverseDirection(m_encoder, reverseDirection, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Set the Samples to Average which specifies the number of samples of the timer
|
|
|
|
|
* to
|
2014-06-13 17:45:10 -04:00
|
|
|
* average when calculating the period. Perform averaging to account for
|
2013-12-15 18:30:16 -05:00
|
|
|
* mechanical imperfections or as oversampling to increase resolution.
|
|
|
|
|
* @param samplesToAverage The number of samples to average from 1 to 127.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::SetSamplesToAverage(int samplesToAverage) {
|
|
|
|
|
if (samplesToAverage < 1 || samplesToAverage > 127) {
|
|
|
|
|
wpi_setWPIErrorWithContext(
|
|
|
|
|
ParameterOutOfRange,
|
|
|
|
|
"Average counter values must be between 1 and 127");
|
|
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
switch (m_encodingType) {
|
|
|
|
|
case k4X:
|
|
|
|
|
setEncoderSamplesToAverage(m_encoder, samplesToAverage, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
break;
|
|
|
|
|
case k1X:
|
|
|
|
|
case k2X:
|
|
|
|
|
m_counter->SetSamplesToAverage(samplesToAverage);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Get the Samples to Average which specifies the number of samples of the timer
|
|
|
|
|
* to
|
2014-06-13 17:45:10 -04:00
|
|
|
* average when calculating the period. Perform averaging to account for
|
2013-12-15 18:30:16 -05:00
|
|
|
* mechanical imperfections or as oversampling to increase resolution.
|
|
|
|
|
* @return SamplesToAverage The number of samples being averaged (from 1 to 127)
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int Encoder::GetSamplesToAverage() const {
|
|
|
|
|
int result = 1;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
switch (m_encodingType) {
|
|
|
|
|
case k4X:
|
|
|
|
|
result = getEncoderSamplesToAverage(m_encoder, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
break;
|
|
|
|
|
case k1X:
|
|
|
|
|
case k2X:
|
|
|
|
|
result = m_counter->GetSamplesToAverage();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Set which parameter of the encoder you are using as a process control
|
|
|
|
|
* variable.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param pidSource An enum to select the parameter.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::SetPIDSourceParameter(PIDSourceParameter pidSource) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
m_pidSource = pidSource;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implement the PIDSource interface.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The current value of the selected source parameter.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Encoder::PIDGet() const {
|
|
|
|
|
if (StatusIsFatal()) return 0.0;
|
|
|
|
|
switch (m_pidSource) {
|
|
|
|
|
case kDistance:
|
|
|
|
|
return GetDistance();
|
|
|
|
|
case kRate:
|
|
|
|
|
return GetRate();
|
|
|
|
|
default:
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-06 16:39:24 -05:00
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Set the index source for the encoder. When this source is activated, the
|
|
|
|
|
* encoder count automatically resets.
|
2015-01-06 16:39:24 -05:00
|
|
|
*
|
|
|
|
|
* @param channel A DIO channel to set as the encoder index
|
|
|
|
|
* @param type The state that will cause the encoder to reset
|
|
|
|
|
*/
|
|
|
|
|
void Encoder::SetIndexSource(uint32_t channel, Encoder::IndexingType type) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
bool activeHigh = (type == kResetWhileHigh) || (type == kResetOnRisingEdge);
|
|
|
|
|
bool edgeSensitive =
|
|
|
|
|
(type == kResetOnFallingEdge) || (type == kResetOnRisingEdge);
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
setEncoderIndexSource(m_encoder, channel, false, activeHigh, edgeSensitive,
|
|
|
|
|
&status);
|
2015-01-06 16:39:24 -05:00
|
|
|
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Set the index source for the encoder. When this source is activated, the
|
|
|
|
|
* encoder count automatically resets.
|
2015-01-06 16:39:24 -05:00
|
|
|
*
|
|
|
|
|
* @param channel A digital source to set as the encoder index
|
|
|
|
|
* @param type The state that will cause the encoder to reset
|
|
|
|
|
*/
|
2015-06-30 15:01:20 -04:00
|
|
|
[[deprecated("Raw pointers are dperecated; use references instead.")]]
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::SetIndexSource(DigitalSource *source,
|
|
|
|
|
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
|
|
|
SetIndexSource(*source, type);
|
2015-01-06 16:39:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Set the index source for the encoder. When this source is activated, the
|
|
|
|
|
* encoder count automatically resets.
|
2015-01-06 16:39:24 -05:00
|
|
|
*
|
|
|
|
|
* @param channel A digital source to set as the encoder index
|
|
|
|
|
* @param type The state that will cause the encoder to reset
|
|
|
|
|
*/
|
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
|
|
|
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;
|
|
|
|
|
bool activeHigh = (type == kResetWhileHigh) || (type == kResetOnRisingEdge);
|
|
|
|
|
bool edgeSensitive =
|
|
|
|
|
(type == kResetOnFallingEdge) || (type == kResetOnRisingEdge);
|
|
|
|
|
|
|
|
|
|
setEncoderIndexSource(m_encoder, source.GetChannelForRouting(),
|
|
|
|
|
source.GetAnalogTriggerForRouting(), activeHigh,
|
|
|
|
|
edgeSensitive, &status);
|
|
|
|
|
wpi_setGlobalErrorWithContext(status, getHALErrorMessage(status));
|
2015-01-06 16:39:24 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
void Encoder::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutNumber("Speed", GetRate());
|
|
|
|
|
m_table->PutNumber("Distance", GetDistance());
|
|
|
|
|
m_table->PutNumber("Distance per Tick", m_distancePerPulse);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::StartLiveWindowMode() {}
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Encoder::StopLiveWindowMode() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
std::string Encoder::GetSmartDashboardType() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (m_encodingType == k4X)
|
|
|
|
|
return "Quadrature Encoder";
|
|
|
|
|
else
|
|
|
|
|
return "Encoder";
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Encoder::InitTable(std::shared_ptr<ITable> subTable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
std::shared_ptr<ITable> Encoder::GetTable() const { return m_table; }
|