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 "Counter.h"
|
|
|
|
|
#include "AnalogTrigger.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"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an instance of a counter object.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This creates a ChipObject counter and initializes status variables
|
|
|
|
|
* appropriately
|
2014-07-29 09:58:15 -07:00
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param mode The counter mode
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::InitCounter(Mode mode) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_counter = initializeCounter(mode, &m_index, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
SetMaxPeriod(.5);
|
2014-12-23 19:47:23 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
HALReport(HALUsageReporting::kResourceType_Counter, m_index, mode);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an instance of a counter where no sources are selected.
|
2015-06-25 15:07:55 -04:00
|
|
|
* They all must be selected by calling functions to specify the upsource and
|
|
|
|
|
* the downsource
|
2013-12-15 18:30:16 -05:00
|
|
|
* independently.
|
2014-07-29 09:58:15 -07:00
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-24 01:06:29 -07:00
|
|
|
Counter::Counter() {
|
2015-06-25 15:07:55 -04:00
|
|
|
InitCounter();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Create an instance of a counter from a Digital Source (such as a Digital
|
|
|
|
|
* Input).
|
|
|
|
|
* This is used if an existing digital input is to be shared by multiple other
|
|
|
|
|
* objects such
|
|
|
|
|
* as encoders or if the Digital Source is not a Digital Input channel (such as
|
|
|
|
|
* an Analog Trigger).
|
2014-07-29 09:58:15 -07:00
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param source A pointer to the existing DigitalSource object. It will be set
|
|
|
|
|
* as the Up Source.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-24 01:06:29 -07:00
|
|
|
Counter::Counter(DigitalSource *source) {
|
2015-06-25 15:07:55 -04:00
|
|
|
InitCounter();
|
|
|
|
|
SetUpSource(source);
|
|
|
|
|
ClearDownSource();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Create an instance of a counter from a Digital Source (such as a Digital
|
|
|
|
|
* Input).
|
|
|
|
|
* This is used if an existing digital input is to be shared by multiple other
|
|
|
|
|
* objects such
|
|
|
|
|
* as encoders or if the Digital Source is not a Digital Input channel (such as
|
|
|
|
|
* an Analog Trigger).
|
2014-12-29 14:09:37 -05:00
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param source A reference to the existing DigitalSource object. It will be
|
|
|
|
|
* set as the Up Source.
|
2014-12-29 14:09:37 -05:00
|
|
|
*/
|
2015-06-24 01:06:29 -07:00
|
|
|
Counter::Counter(DigitalSource &source) {
|
2015-06-25 15:07:55 -04:00
|
|
|
InitCounter();
|
|
|
|
|
SetUpSource(&source);
|
|
|
|
|
ClearDownSource();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an instance of a Counter object.
|
2014-06-13 17:45:10 -04:00
|
|
|
* Create an up-Counter instance given a channel.
|
2014-07-29 09:58:15 -07:00
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param channel The DIO channel to use as the up source. 0-9 are on-board,
|
|
|
|
|
* 10-25 are on the MXP
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-24 01:06:29 -07:00
|
|
|
Counter::Counter(int32_t channel) {
|
2015-06-25 15:07:55 -04:00
|
|
|
InitCounter();
|
|
|
|
|
SetUpSource(channel);
|
|
|
|
|
ClearDownSource();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an instance of a Counter object.
|
|
|
|
|
* Create an instance of a simple up-Counter given an analog trigger.
|
|
|
|
|
* Use the trigger state output from the analog trigger.
|
2014-07-29 09:58:15 -07:00
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param trigger The pointer to the existing AnalogTrigger object.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-24 01:06:29 -07:00
|
|
|
Counter::Counter(AnalogTrigger *trigger) {
|
2015-06-25 15:07:55 -04:00
|
|
|
InitCounter();
|
|
|
|
|
SetUpSource(trigger->CreateOutput(kState));
|
|
|
|
|
ClearDownSource();
|
|
|
|
|
m_allocatedUpSource = true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Create an instance of a Counter object.
|
|
|
|
|
* Create an instance of a simple up-Counter given an analog trigger.
|
|
|
|
|
* Use the trigger state output from the analog trigger.
|
|
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
|
|
|
|
* @param trigger The reference to the existing AnalogTrigger object.
|
|
|
|
|
*/
|
2015-06-24 01:06:29 -07:00
|
|
|
Counter::Counter(AnalogTrigger &trigger) {
|
2015-06-25 15:07:55 -04:00
|
|
|
InitCounter();
|
|
|
|
|
SetUpSource(trigger.CreateOutput(kState));
|
|
|
|
|
ClearDownSource();
|
|
|
|
|
m_allocatedUpSource = true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Create an instance of a Counter object.
|
|
|
|
|
* Creates a full up-down counter given two Digital Sources
|
|
|
|
|
* @param encodingType The quadrature decoding mode (1x or 2x)
|
|
|
|
|
* @param upSource The pointer to the DigitalSource to set as the up source
|
|
|
|
|
* @param downSource The pointer to the DigitalSource to set as the down source
|
|
|
|
|
* @param inverted True to invert the output (reverse the direction)
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
Counter::Counter(EncodingType encodingType, DigitalSource *upSource,
|
2015-06-24 01:06:29 -07:00
|
|
|
DigitalSource *downSource, bool inverted) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (encodingType != k1X && encodingType != k2X) {
|
|
|
|
|
wpi_setWPIErrorWithContext(
|
|
|
|
|
ParameterOutOfRange,
|
|
|
|
|
"Counter only supports 1X and 2X quadrature decoding.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
InitCounter(kExternalDirection);
|
|
|
|
|
SetUpSource(upSource);
|
|
|
|
|
SetDownSource(downSource);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
if (encodingType == k1X) {
|
|
|
|
|
SetUpSourceEdge(true, false);
|
|
|
|
|
setCounterAverageSize(m_counter, 1, &status);
|
|
|
|
|
} else {
|
|
|
|
|
SetUpSourceEdge(true, true);
|
|
|
|
|
setCounterAverageSize(m_counter, 2, &status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
SetDownSourceEdge(inverted, true);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete the Counter object.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Counter::~Counter() {
|
|
|
|
|
SetUpdateWhenEmpty(true);
|
|
|
|
|
if (m_allocatedUpSource) {
|
|
|
|
|
delete m_upSource;
|
2015-06-23 04:49:51 -07:00
|
|
|
m_upSource = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
if (m_allocatedDownSource) {
|
|
|
|
|
delete m_downSource;
|
2015-06-23 04:49:51 -07:00
|
|
|
m_downSource = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
freeCounter(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2015-06-23 04:49:51 -07:00
|
|
|
m_counter = nullptr;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the upsource for the counter as a digital input channel.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param channel The DIO channel to use as the up source. 0-9 are on-board,
|
|
|
|
|
* 10-25 are on the MXP
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpSource(int32_t channel) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
SetUpSource(new DigitalInput(channel));
|
|
|
|
|
m_allocatedUpSource = true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the up counting source to be an analog trigger.
|
|
|
|
|
* @param analogTrigger The analog trigger object that is used for the Up Source
|
|
|
|
|
* @param triggerType The analog trigger output that will trigger the counter.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpSource(AnalogTrigger *analogTrigger,
|
|
|
|
|
AnalogTriggerType triggerType) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
SetUpSource(analogTrigger->CreateOutput(triggerType));
|
|
|
|
|
m_allocatedUpSource = true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the up counting source to be an analog trigger.
|
|
|
|
|
* @param analogTrigger The analog trigger object that is used for the Up Source
|
|
|
|
|
* @param triggerType The analog trigger output that will trigger the counter.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpSource(AnalogTrigger &analogTrigger,
|
|
|
|
|
AnalogTriggerType triggerType) {
|
|
|
|
|
SetUpSource(&analogTrigger, triggerType);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the source object that causes the counter to count up.
|
|
|
|
|
* Set the up counting DigitalSource.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param source Pointer to the DigitalSource object to set as the up source
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpSource(DigitalSource *source) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
if (m_allocatedUpSource) {
|
|
|
|
|
delete m_upSource;
|
2015-06-23 04:49:51 -07:00
|
|
|
m_upSource = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
m_allocatedUpSource = false;
|
|
|
|
|
}
|
|
|
|
|
m_upSource = source;
|
|
|
|
|
if (m_upSource->StatusIsFatal()) {
|
|
|
|
|
CloneError(m_upSource);
|
|
|
|
|
} else {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterUpSource(m_counter, source->GetChannelForRouting(),
|
|
|
|
|
source->GetAnalogTriggerForRouting(), &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the source object that causes the counter to count up.
|
|
|
|
|
* Set the up counting DigitalSource.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param source Reference to the DigitalSource object to set as the up source
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpSource(DigitalSource &source) { SetUpSource(&source); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the edge sensitivity on an up counting source.
|
2014-12-29 14:09:37 -05:00
|
|
|
* Set the up source to either detect rising edges or falling edges or both.
|
|
|
|
|
* @param risingEdge True to trigger on rising edges
|
|
|
|
|
* @param fallingEdge True to trigger on falling edges
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpSourceEdge(bool risingEdge, bool fallingEdge) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_upSource == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(
|
|
|
|
|
NullParameter,
|
2015-06-23 04:49:51 -07:00
|
|
|
"Must set non-nullptr UpSource before setting UpSourceEdge");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterUpSourceEdge(m_counter, risingEdge, fallingEdge, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable the up counting source to the counter.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::ClearUpSource() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
if (m_allocatedUpSource) {
|
|
|
|
|
delete m_upSource;
|
2015-06-23 04:49:51 -07:00
|
|
|
m_upSource = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
m_allocatedUpSource = false;
|
|
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
clearCounterUpSource(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the down counting source to be a digital input channel.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param channel The DIO channel to use as the up source. 0-9 are on-board,
|
|
|
|
|
* 10-25 are on the MXP
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetDownSource(int32_t channel) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
SetDownSource(new DigitalInput(channel));
|
|
|
|
|
m_allocatedDownSource = true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the down counting source to be an analog trigger.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param analogTrigger The analog trigger object that is used for the Down
|
|
|
|
|
* Source
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param triggerType The analog trigger output that will trigger the counter.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetDownSource(AnalogTrigger *analogTrigger,
|
|
|
|
|
AnalogTriggerType triggerType) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
SetDownSource(analogTrigger->CreateOutput(triggerType));
|
|
|
|
|
m_allocatedDownSource = true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the down counting source to be an analog trigger.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param analogTrigger The analog trigger object that is used for the Down
|
|
|
|
|
* Source
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param triggerType The analog trigger output that will trigger the counter.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetDownSource(AnalogTrigger &analogTrigger,
|
|
|
|
|
AnalogTriggerType triggerType) {
|
|
|
|
|
SetDownSource(&analogTrigger, triggerType);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the source object that causes the counter to count down.
|
|
|
|
|
* Set the down counting DigitalSource.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param source Pointer to the DigitalSource object to set as the down source
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetDownSource(DigitalSource *source) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
if (m_allocatedDownSource) {
|
|
|
|
|
delete m_downSource;
|
2015-06-23 04:49:51 -07:00
|
|
|
m_downSource = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
m_allocatedDownSource = false;
|
|
|
|
|
}
|
|
|
|
|
m_downSource = source;
|
|
|
|
|
if (m_downSource->StatusIsFatal()) {
|
|
|
|
|
CloneError(m_downSource);
|
|
|
|
|
} else {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterDownSource(m_counter, source->GetChannelForRouting(),
|
|
|
|
|
source->GetAnalogTriggerForRouting(), &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the source object that causes the counter to count down.
|
|
|
|
|
* Set the down counting DigitalSource.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param source Reference to the DigitalSource object to set as the down source
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetDownSource(DigitalSource &source) { SetDownSource(&source); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the edge sensitivity on a down counting source.
|
|
|
|
|
* Set the down source to either detect rising edges or falling edges.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param risingEdge True to trigger on rising edges
|
|
|
|
|
* @param fallingEdge True to trigger on falling edges
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetDownSourceEdge(bool risingEdge, bool fallingEdge) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_downSource == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(
|
|
|
|
|
NullParameter,
|
2015-06-23 04:49:51 -07:00
|
|
|
"Must set non-nullptr DownSource before setting DownSourceEdge");
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterDownSourceEdge(m_counter, risingEdge, fallingEdge, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable the down counting source to the counter.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::ClearDownSource() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
if (m_allocatedDownSource) {
|
|
|
|
|
delete m_downSource;
|
2015-06-23 04:49:51 -07:00
|
|
|
m_downSource = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
m_allocatedDownSource = false;
|
|
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
clearCounterDownSource(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set standard up / down counting mode on this counter.
|
|
|
|
|
* Up and down counts are sourced independently from two inputs.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpDownCounterMode() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterUpDownMode(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set external direction mode on this counter.
|
|
|
|
|
* Counts are sourced on the Up counter input.
|
|
|
|
|
* The Down counter input represents the direction to count.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetExternalDirectionMode() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterExternalDirectionMode(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set Semi-period mode on this counter.
|
2014-06-13 17:45:10 -04:00
|
|
|
* Counts up on both rising and falling edges.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetSemiPeriodMode(bool highSemiPeriod) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterSemiPeriodMode(m_counter, highSemiPeriod, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Configure the counter to count in up or down based on the length of the input
|
|
|
|
|
* pulse.
|
2013-12-15 18:30:16 -05:00
|
|
|
* This mode is most useful for direction sensitive gear tooth sensors.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param threshold The pulse length beyond which the counter counts the
|
|
|
|
|
* opposite direction. Units are seconds.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetPulseLengthMode(float threshold) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterPulseLengthMode(m_counter, threshold, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
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 Counter::GetSamplesToAverage() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
int32_t samples = getCounterSamplesToAverage(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return samples;
|
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 Counter::SetSamplesToAverage(int samplesToAverage) {
|
|
|
|
|
if (samplesToAverage < 1 || samplesToAverage > 127) {
|
|
|
|
|
wpi_setWPIErrorWithContext(
|
|
|
|
|
ParameterOutOfRange,
|
|
|
|
|
"Average counter values must be between 1 and 127");
|
|
|
|
|
}
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterSamplesToAverage(m_counter, samplesToAverage, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the current counter value.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Read the value at this instant. It may still be running, so it reflects the
|
|
|
|
|
* current value. Next
|
2013-12-15 18:30:16 -05:00
|
|
|
* time it is read, it might have a different value.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t Counter::Get() const {
|
|
|
|
|
if (StatusIsFatal()) return 0;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
int32_t value = getCounter(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the Counter to zero.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Set the counter value to zero. This doesn't effect the running state of the
|
|
|
|
|
* counter, just sets
|
2013-12-15 18:30:16 -05:00
|
|
|
* the current value to zero.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::Reset() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
resetCounter(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2013-12-15 18:30:16 -05:00
|
|
|
* Get the Period of the most recent count.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns the time interval of the most recent count. This can be used for
|
|
|
|
|
* velocity calculations
|
2013-12-15 18:30:16 -05:00
|
|
|
* to determine shaft speed.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @returns The period between the last two pulses in units of seconds.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Counter::GetPeriod() const {
|
|
|
|
|
if (StatusIsFatal()) return 0.0;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
double value = getCounterPeriod(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the maximum period where the device is still considered "moving".
|
2015-06-25 15:07:55 -04:00
|
|
|
* Sets the maximum period where the device is considered moving. This value is
|
|
|
|
|
* used to determine
|
2013-12-15 18:30:16 -05:00
|
|
|
* the "stopped" state of the counter using the GetStopped method.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param maxPeriod The maximum period where the counted device is considered
|
|
|
|
|
* moving in
|
2013-12-15 18:30:16 -05:00
|
|
|
* seconds.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetMaxPeriod(double maxPeriod) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterMaxPeriod(m_counter, maxPeriod, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Select whether you want to continue updating the event timer output when
|
|
|
|
|
* there are no samples captured.
|
|
|
|
|
* The output of the event timer has a buffer of periods that are averaged and
|
|
|
|
|
* posted to
|
|
|
|
|
* a register on the FPGA. When the timer detects that the event source has
|
|
|
|
|
* stopped
|
|
|
|
|
* (based on the MaxPeriod) the buffer of samples to be averaged is emptied. If
|
|
|
|
|
* you
|
|
|
|
|
* enable the update when empty, you will be notified of the stopped source and
|
|
|
|
|
* the event
|
|
|
|
|
* time will report 0 samples. If you disable update when empty, the most
|
|
|
|
|
* recent average
|
|
|
|
|
* will remain on the output until a new sample is acquired. You will never see
|
|
|
|
|
* 0 samples
|
|
|
|
|
* output (except when there have been no events since an FPGA reset) and you
|
|
|
|
|
* will likely not
|
|
|
|
|
* see the stopped bit become true (since it is updated at the end of an average
|
|
|
|
|
* and there are
|
2013-12-15 18:30:16 -05:00
|
|
|
* no samples to average).
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param enabled True to enable update when empty
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetUpdateWhenEmpty(bool enabled) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterUpdateWhenEmpty(m_counter, enabled, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the clock is stopped.
|
2015-06-25 15:07:55 -04:00
|
|
|
* Determine if the clocked input is stopped based on the MaxPeriod value set
|
|
|
|
|
* using the
|
|
|
|
|
* SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the device (and
|
|
|
|
|
* counter) are
|
2013-12-15 18:30:16 -05:00
|
|
|
* assumed to be stopped and it returns true.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return Returns true if the most recent counter period exceeds the MaxPeriod
|
|
|
|
|
* value set by
|
2013-12-15 18:30:16 -05:00
|
|
|
* SetMaxPeriod.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Counter::GetStopped() const {
|
|
|
|
|
if (StatusIsFatal()) return false;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
bool value = getCounterStopped(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The last direction the counter value changed.
|
|
|
|
|
* @return The last direction the counter value changed.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Counter::GetDirection() const {
|
|
|
|
|
if (StatusIsFatal()) return false;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
bool value = getCounterDirection(m_counter, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the Counter to return reversed sensing on the direction.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This allows counters to change the direction they are counting in the case of
|
|
|
|
|
* 1X and 2X
|
2013-12-15 18:30:16 -05:00
|
|
|
* quadrature encoding only. Any other counter mode isn't supported.
|
|
|
|
|
* @param reverseDirection true if the value counted should be negated.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::SetReverseDirection(bool reverseDirection) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setCounterReverseDirection(m_counter, reverseDirection, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Counter::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutNumber("Value", Get());
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::StartLiveWindowMode() {}
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Counter::StopLiveWindowMode() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string Counter::GetSmartDashboardType() const { return "Counter"; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
void Counter::InitTable(ITable *subTable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ITable *Counter::GetTable() const { return m_table; }
|