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 "AnalogTrigger.h"
|
|
|
|
|
|
2014-06-12 18:07:45 -04:00
|
|
|
#include "AnalogInput.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"
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-13 17:45:10 -04:00
|
|
|
* Initialize an analog trigger from a channel.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogTrigger::InitTrigger(uint32_t channel) {
|
|
|
|
|
void *port = getPort(channel);
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
uint32_t index = 0;
|
|
|
|
|
m_trigger = initializeAnalogTrigger(port, &index, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
m_index = index;
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
HALReport(HALUsageReporting::kResourceType_AnalogTrigger, channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor for an analog trigger given a channel number.
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param channel The channel number on the roboRIO to represent. 0-3 are
|
|
|
|
|
* on-board 4-7 are on the MXP port.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTrigger::AnalogTrigger(int32_t channel) { InitTrigger(channel); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
2014-06-13 17:45:10 -04:00
|
|
|
* Construct an analog trigger given an analog input.
|
|
|
|
|
* This should be used in the case of sharing an analog channel between the
|
|
|
|
|
* trigger and an analog input object.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param channel The pointer to the existing AnalogInput object
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTrigger::AnalogTrigger(AnalogInput *input) {
|
|
|
|
|
InitTrigger(input->GetChannel());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTrigger::~AnalogTrigger() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
cleanAnalogTrigger(m_trigger, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the upper and lower limits of the analog trigger.
|
2015-06-25 15:07:55 -04:00
|
|
|
* The limits are given in ADC codes. If oversampling is used, the units must
|
|
|
|
|
* be scaled
|
2013-12-15 18:30:16 -05:00
|
|
|
* appropriately.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param lower The lower limit of the trigger in ADC codes (12-bit values).
|
|
|
|
|
* @param upper The upper limit of the trigger in ADC codes (12-bit values).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogTrigger::SetLimitsRaw(int32_t lower, int32_t upper) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setAnalogTriggerLimitsRaw(m_trigger, lower, upper, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the upper and lower limits of the analog trigger.
|
|
|
|
|
* The limits are given as floating point voltage values.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param lower The lower limit of the trigger in Volts.
|
|
|
|
|
* @param upper The upper limit of the trigger in Volts.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogTrigger::SetLimitsVoltage(float lower, float upper) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setAnalogTriggerLimitsVoltage(m_trigger, lower, upper, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configure the analog trigger to use the averaged vs. raw values.
|
2015-06-25 15:07:55 -04:00
|
|
|
* If the value is true, then the averaged value is selected for the analog
|
|
|
|
|
* trigger, otherwise
|
2013-12-15 18:30:16 -05:00
|
|
|
* the immediate value is used.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param useAveragedValue If true, use the Averaged value, otherwise use the
|
|
|
|
|
* instantaneous reading
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogTrigger::SetAveraged(bool useAveragedValue) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setAnalogTriggerAveraged(m_trigger, useAveragedValue, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configure the analog trigger to use a filtered value.
|
2015-06-25 15:07:55 -04:00
|
|
|
* The analog trigger will operate with a 3 point average rejection filter. This
|
|
|
|
|
* is designed to
|
|
|
|
|
* help with 360 degree pot applications for the period where the pot crosses
|
|
|
|
|
* through zero.
|
|
|
|
|
* @param useFilteredValue If true, use the 3 point rejection filter, otherwise
|
|
|
|
|
* use the unfiltered value
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void AnalogTrigger::SetFiltered(bool useFilteredValue) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setAnalogTriggerFiltered(m_trigger, useFilteredValue, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the index of the analog trigger.
|
|
|
|
|
* This is the FPGA index of this analog trigger instance.
|
|
|
|
|
* @return The index of the analog trigger.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
uint32_t AnalogTrigger::GetIndex() {
|
|
|
|
|
if (StatusIsFatal()) return ~0ul;
|
|
|
|
|
return m_index;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the InWindow output of the analog trigger.
|
|
|
|
|
* True if the analog input is between the upper and lower limits.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return True if the analog input is between the upper and lower limits.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool AnalogTrigger::GetInWindow() {
|
|
|
|
|
if (StatusIsFatal()) return false;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
bool result = getAnalogTriggerInWindow(m_trigger, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return result;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the TriggerState output of the analog trigger.
|
|
|
|
|
* True if above upper limit.
|
|
|
|
|
* False if below lower limit.
|
|
|
|
|
* If in Hysteresis, maintain previous state.
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return True if above upper limit. False if below lower limit. If in
|
|
|
|
|
* Hysteresis, maintain previous state.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool AnalogTrigger::GetTriggerState() {
|
|
|
|
|
if (StatusIsFatal()) return false;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
bool result = getAnalogTriggerTriggerState(m_trigger, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return result;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an AnalogTriggerOutput object.
|
|
|
|
|
* Gets an output object that can be used for routing.
|
|
|
|
|
* Caller is responsible for deleting the AnalogTriggerOutput object.
|
|
|
|
|
* @param type An enum of the type of output object to create.
|
|
|
|
|
* @return A pointer to a new AnalogTriggerOutput object.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
AnalogTriggerOutput *AnalogTrigger::CreateOutput(AnalogTriggerType type) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (StatusIsFatal()) return nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
return new AnalogTriggerOutput(this, type);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|