Add FPGA Duty Cycle support (#1987)

This commit is contained in:
Thad House
2019-11-01 23:41:30 -07:00
committed by Peter Johnson
parent 509819d83f
commit 1d695a1660
42 changed files with 1744 additions and 72 deletions

View File

@@ -12,6 +12,7 @@
#include <hal/HAL.h>
#include "frc/AnalogInput.h"
#include "frc/DutyCycle.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableRegistry.h"
@@ -26,19 +27,31 @@ AnalogTrigger::AnalogTrigger(int channel)
AnalogTrigger::AnalogTrigger(AnalogInput* input) {
m_analogInput = input;
int32_t status = 0;
int index = 0;
m_trigger = HAL_InitializeAnalogTrigger(input->m_port, &index, &status);
m_trigger = HAL_InitializeAnalogTrigger(input->m_port, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
m_index = std::numeric_limits<int>::max();
m_trigger = HAL_kInvalidHandle;
return;
}
m_index = index;
int index = GetIndex();
HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, m_index + 1);
SendableRegistry::GetInstance().AddLW(this, "AnalogTrigger",
input->GetChannel());
HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1);
SendableRegistry::GetInstance().AddLW(this, "AnalogTrigger", index);
}
AnalogTrigger::AnalogTrigger(DutyCycle* input) {
m_dutyCycle = input;
int32_t status = 0;
m_trigger = HAL_InitializeAnalogTriggerDutyCycle(input->m_handle, &status);
if (status != 0) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
m_trigger = HAL_kInvalidHandle;
return;
}
int index = GetIndex();
HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1);
SendableRegistry::GetInstance().AddLW(this, "AnalogTrigger", index);
}
AnalogTrigger::~AnalogTrigger() {
@@ -53,9 +66,9 @@ AnalogTrigger::~AnalogTrigger() {
AnalogTrigger::AnalogTrigger(AnalogTrigger&& rhs)
: ErrorBase(std::move(rhs)),
SendableHelper(std::move(rhs)),
m_index(std::move(rhs.m_index)),
m_trigger(std::move(rhs.m_trigger)) {
std::swap(m_analogInput, rhs.m_analogInput);
std::swap(m_dutyCycle, rhs.m_dutyCycle);
std::swap(m_ownsAnalog, rhs.m_ownsAnalog);
}
@@ -63,9 +76,9 @@ AnalogTrigger& AnalogTrigger::operator=(AnalogTrigger&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableHelper::operator=(std::move(rhs));
m_index = std::move(rhs.m_index);
m_trigger = std::move(rhs.m_trigger);
std::swap(m_analogInput, rhs.m_analogInput);
std::swap(m_dutyCycle, rhs.m_dutyCycle);
std::swap(m_ownsAnalog, rhs.m_ownsAnalog);
return *this;
@@ -78,6 +91,13 @@ void AnalogTrigger::SetLimitsVoltage(double lower, double upper) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
}
void AnalogTrigger::SetLimitsDutyCycle(double lower, double upper) {
if (StatusIsFatal()) return;
int32_t status = 0;
HAL_SetAnalogTriggerLimitsDutyCycle(m_trigger, lower, upper, &status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
}
void AnalogTrigger::SetLimitsRaw(int lower, int upper) {
if (StatusIsFatal()) return;
int32_t status = 0;
@@ -101,7 +121,10 @@ void AnalogTrigger::SetFiltered(bool useFilteredValue) {
int AnalogTrigger::GetIndex() const {
if (StatusIsFatal()) return -1;
return m_index;
int32_t status = 0;
auto ret = HAL_GetAnalogTriggerFPGAIndex(m_trigger, &status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
return ret;
}
bool AnalogTrigger::GetInWindow() {

View File

@@ -33,7 +33,7 @@ AnalogTriggerType AnalogTriggerOutput::GetAnalogTriggerTypeForRouting() const {
bool AnalogTriggerOutput::IsAnalogTrigger() const { return true; }
int AnalogTriggerOutput::GetChannel() const { return m_trigger->m_index; }
int AnalogTriggerOutput::GetChannel() const { return m_trigger->GetIndex(); }
void AnalogTriggerOutput::InitSendable(SendableBuilder&) {}

View File

@@ -0,0 +1,99 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "frc/DutyCycle.h"
#include <hal/DutyCycle.h>
#include <hal/FRCUsageReporting.h>
#include <hal/HALBase.h>
#include "frc/DigitalSource.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
using namespace frc;
DutyCycle::DutyCycle(DigitalSource* source)
: m_source{source, NullDeleter<DigitalSource>()} {
if (m_source == nullptr) {
wpi_setWPIError(NullParameter);
} else {
InitDutyCycle();
}
}
DutyCycle::DutyCycle(DigitalSource& source)
: m_source{&source, NullDeleter<DigitalSource>()} {
InitDutyCycle();
}
DutyCycle::DutyCycle(std::shared_ptr<DigitalSource> source)
: m_source{std::move(source)} {
if (m_source == nullptr) {
wpi_setWPIError(NullParameter);
} else {
InitDutyCycle();
}
}
DutyCycle::~DutyCycle() { HAL_FreeDutyCycle(m_handle); }
void DutyCycle::InitDutyCycle() {
int32_t status = 0;
m_handle =
HAL_InitializeDutyCycle(m_source->GetPortHandleForRouting(),
static_cast<HAL_AnalogTriggerType>(
m_source->GetAnalogTriggerTypeForRouting()),
&status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
int index = GetFPGAIndex();
HAL_Report(HALUsageReporting::kResourceType_DutyCycle, index + 1);
SendableRegistry::GetInstance().AddLW(this, "Duty Cycle", index);
}
int DutyCycle::GetFPGAIndex() const {
int32_t status = 0;
auto retVal = HAL_GetDutyCycleFPGAIndex(m_handle, &status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
return retVal;
}
int DutyCycle::GetFrequency() const {
int32_t status = 0;
auto retVal = HAL_GetDutyCycleFrequency(m_handle, &status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
return retVal;
}
double DutyCycle::GetOutput() const {
int32_t status = 0;
auto retVal = HAL_GetDutyCycleOutput(m_handle, &status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
return retVal;
}
unsigned int DutyCycle::GetOutputRaw() const {
int32_t status = 0;
auto retVal = HAL_GetDutyCycleOutput(m_handle, &status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
return retVal;
}
unsigned int DutyCycle::GetOutputScaleFactor() const {
int32_t status = 0;
auto retVal = HAL_GetDutyCycleOutputScaleFactor(m_handle, &status);
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
return retVal;
}
void DutyCycle::InitSendable(SendableBuilder& builder) {
builder.SetSmartDashboardType("Duty Cycle");
builder.AddDoubleProperty("Frequency",
[this] { return this->GetFrequency(); }, nullptr);
builder.AddDoubleProperty("Output", [this] { return this->GetOutput(); },
nullptr);
}

View File

@@ -19,6 +19,7 @@
namespace frc {
class AnalogInput;
class DutyCycle;
class SendableBuilder;
class AnalogTrigger : public ErrorBase,
@@ -45,6 +46,13 @@ class AnalogTrigger : public ErrorBase,
*/
explicit AnalogTrigger(AnalogInput* channel);
/**
* Construct an analog trigger given a duty cycle input.
*
* @param channel The pointer to the existing DutyCycle object
*/
explicit AnalogTrigger(DutyCycle* dutyCycle);
~AnalogTrigger() override;
AnalogTrigger(AnalogTrigger&& rhs);
@@ -60,6 +68,16 @@ class AnalogTrigger : public ErrorBase,
*/
void SetLimitsVoltage(double lower, double upper);
/**
* Set the upper and lower duty cycle limits of the analog trigger.
*
* The limits are given as floating point values between 0 and 1.
*
* @param lower The lower limit of the trigger in percentage.
* @param upper The upper limit of the trigger in percentage.
*/
void SetLimitsDutyCycle(double lower, double upper);
/**
* Set the upper and lower limits of the analog trigger.
*
@@ -82,6 +100,17 @@ class AnalogTrigger : public ErrorBase,
*/
void SetAveraged(bool useAveragedValue);
/**
* Configure the analog trigger to use the duty cycle vs. raw values.
*
* If the value is true, then the duty cycle value is selected for the analog
* trigger, otherwise the immediate value is used.
*
* @param useDutyCycle If true, use the duty cycle value, otherwise use the
* instantaneous reading
*/
void SetDutyCycle(bool useDutyCycle);
/**
* Configure the analog trigger to use a filtered value.
*
@@ -139,9 +168,9 @@ class AnalogTrigger : public ErrorBase,
void InitSendable(SendableBuilder& builder) override;
private:
int m_index;
hal::Handle<HAL_AnalogTriggerHandle> m_trigger;
AnalogInput* m_analogInput = nullptr;
DutyCycle* m_dutyCycle = nullptr;
bool m_ownsAnalog = false;
};

View File

@@ -0,0 +1,124 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <memory>
#include <hal/Types.h>
#include "frc/ErrorBase.h"
#include "frc/smartdashboard/Sendable.h"
#include "frc/smartdashboard/SendableHelper.h"
namespace frc {
class DigitalSource;
class AnalogTrigger;
/**
* Class to read a duty cycle PWM input.
*
* <p>PWM input signals are specified with a frequency and a ratio of high to
* low in that frequency. There are 8 of these in the roboRIO, and they can be
* attached to any DigitalSource.
*
* <p>These can be combined as the input of an AnalogTrigger to a Counter in
* order to implement rollover checking.
*
*/
class DutyCycle : public ErrorBase,
public Sendable,
public SendableHelper<DutyCycle> {
friend class AnalogTrigger;
public:
/**
* Constructs a DutyCycle input from a DigitalSource input.
*
* <p> This class does not own the inputted source.
*
* @param source The DigitalSource to use.
*/
explicit DutyCycle(DigitalSource& source);
/**
* Constructs a DutyCycle input from a DigitalSource input.
*
* <p> This class does not own the inputted source.
*
* @param source The DigitalSource to use.
*/
explicit DutyCycle(DigitalSource* source);
/**
* Constructs a DutyCycle input from a DigitalSource input.
*
* <p> This class does not own the inputted source.
*
* @param source The DigitalSource to use.
*/
explicit DutyCycle(std::shared_ptr<DigitalSource> source);
/**
* Close the DutyCycle and free all resources.
*/
~DutyCycle() override;
DutyCycle(DutyCycle&&) = default;
DutyCycle& operator=(DutyCycle&&) = default;
/**
* Get the frequency of the duty cycle signal.
*
* @return frequency in Hertz
*/
int GetFrequency() const;
/**
* Get the output ratio of the duty cycle signal.
*
* <p> 0 means always low, 1 means always high.
*
* @return output ratio between 0 and 1
*/
double GetOutput() const;
/**
* Get the raw output ratio of the duty cycle signal.
*
* <p> 0 means always low, an output equal to
* GetOutputScaleFactor() means always high.
*
* @return output ratio in raw units
*/
unsigned int GetOutputRaw() const;
/**
* Get the scale factor of the output.
*
* <p> An output equal to this value is always high, and then linearly scales
* down to 0. Divide the result of getOutputRaw by this in order to get the
* percentage between 0 and 1.
*
* @return the output scale factor
*/
unsigned int GetOutputScaleFactor() const;
/**
* Get the FPGA index for the DutyCycle.
*
* @return the FPGA index
*/
int GetFPGAIndex() const;
protected:
void InitSendable(SendableBuilder& builder) override;
private:
void InitDutyCycle();
std::shared_ptr<DigitalSource> m_source;
hal::Handle<HAL_DutyCycleHandle> m_handle;
};
} // namespace frc