mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
[hal] Add systemcore duty cycle (#7682)
This commit is contained in:
@@ -4,50 +4,37 @@
|
||||
|
||||
#include "frc/DutyCycle.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <hal/DutyCycle.h>
|
||||
#include <hal/FRCUsageReporting.h>
|
||||
#include <hal/HALBase.h>
|
||||
#include <wpi/NullDeleter.h>
|
||||
#include <wpi/StackTrace.h>
|
||||
#include <wpi/sendable/SendableBuilder.h>
|
||||
|
||||
#include "frc/DigitalSource.h"
|
||||
#include "frc/Errors.h"
|
||||
#include "frc/SensorUtil.h"
|
||||
|
||||
using namespace frc;
|
||||
|
||||
DutyCycle::DutyCycle(DigitalSource* source)
|
||||
: m_source{source, wpi::NullDeleter<DigitalSource>()} {
|
||||
if (!m_source) {
|
||||
throw FRC_MakeError(err::NullParameter, "source");
|
||||
}
|
||||
InitDutyCycle();
|
||||
}
|
||||
|
||||
DutyCycle::DutyCycle(DigitalSource& source)
|
||||
: m_source{&source, wpi::NullDeleter<DigitalSource>()} {
|
||||
InitDutyCycle();
|
||||
}
|
||||
|
||||
DutyCycle::DutyCycle(std::shared_ptr<DigitalSource> source)
|
||||
: m_source{std::move(source)} {
|
||||
if (!m_source) {
|
||||
throw FRC_MakeError(err::NullParameter, "source");
|
||||
DutyCycle::DutyCycle(int channel) : m_channel{channel} {
|
||||
if (!SensorUtil::CheckDigitalChannel(channel)) {
|
||||
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
|
||||
}
|
||||
InitDutyCycle();
|
||||
}
|
||||
|
||||
void DutyCycle::InitDutyCycle() {
|
||||
int32_t status = 0;
|
||||
m_handle =
|
||||
HAL_InitializeDutyCycle(m_source->GetPortHandleForRouting(),
|
||||
static_cast<HAL_AnalogTriggerType>(
|
||||
m_source->GetAnalogTriggerTypeForRouting()),
|
||||
&status);
|
||||
std::string stackTrace = wpi::GetStackTrace(1);
|
||||
m_handle = HAL_InitializeDutyCycle(HAL_GetPort(m_channel), stackTrace.c_str(),
|
||||
&status);
|
||||
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
||||
int index = GetFPGAIndex();
|
||||
HAL_Report(HALUsageReporting::kResourceType_DutyCycle, index + 1);
|
||||
wpi::SendableRegistry::AddLW(this, "Duty Cycle", index);
|
||||
HAL_Report(HALUsageReporting::kResourceType_DutyCycle, m_channel + 1);
|
||||
wpi::SendableRegistry::AddLW(this, "Duty Cycle", m_channel);
|
||||
}
|
||||
|
||||
int DutyCycle::GetFPGAIndex() const {
|
||||
@@ -86,7 +73,7 @@ unsigned int DutyCycle::GetOutputScaleFactor() const {
|
||||
}
|
||||
|
||||
int DutyCycle::GetSourceChannel() const {
|
||||
return m_source->GetChannel();
|
||||
return m_channel;
|
||||
}
|
||||
|
||||
void DutyCycle::InitSendable(wpi::SendableBuilder& builder) {
|
||||
|
||||
@@ -18,8 +18,7 @@
|
||||
using namespace frc;
|
||||
|
||||
DutyCycleEncoder::DutyCycleEncoder(int channel)
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(
|
||||
std::make_shared<DigitalInput>(channel))} {
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(channel)} {
|
||||
Init(1.0, 0.0);
|
||||
}
|
||||
|
||||
@@ -38,25 +37,9 @@ DutyCycleEncoder::DutyCycleEncoder(std::shared_ptr<DutyCycle> dutyCycle)
|
||||
Init(1.0, 0.0);
|
||||
}
|
||||
|
||||
DutyCycleEncoder::DutyCycleEncoder(DigitalSource& digitalSource)
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(digitalSource)} {
|
||||
Init(1.0, 0.0);
|
||||
}
|
||||
|
||||
DutyCycleEncoder::DutyCycleEncoder(DigitalSource* digitalSource)
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(digitalSource)} {
|
||||
Init(1.0, 0.0);
|
||||
}
|
||||
|
||||
DutyCycleEncoder::DutyCycleEncoder(std::shared_ptr<DigitalSource> digitalSource)
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(digitalSource)} {
|
||||
Init(1.0, 0.0);
|
||||
}
|
||||
|
||||
DutyCycleEncoder::DutyCycleEncoder(int channel, double fullRange,
|
||||
double expectedZero)
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(
|
||||
std::make_shared<DigitalInput>(channel))} {
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(channel)} {
|
||||
Init(fullRange, expectedZero);
|
||||
}
|
||||
|
||||
@@ -78,24 +61,6 @@ DutyCycleEncoder::DutyCycleEncoder(std::shared_ptr<DutyCycle> dutyCycle,
|
||||
Init(fullRange, expectedZero);
|
||||
}
|
||||
|
||||
DutyCycleEncoder::DutyCycleEncoder(DigitalSource& digitalSource,
|
||||
double fullRange, double expectedZero)
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(digitalSource)} {
|
||||
Init(fullRange, expectedZero);
|
||||
}
|
||||
|
||||
DutyCycleEncoder::DutyCycleEncoder(DigitalSource* digitalSource,
|
||||
double fullRange, double expectedZero)
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(digitalSource)} {
|
||||
Init(fullRange, expectedZero);
|
||||
}
|
||||
|
||||
DutyCycleEncoder::DutyCycleEncoder(std::shared_ptr<DigitalSource> digitalSource,
|
||||
double fullRange, double expectedZero)
|
||||
: m_dutyCycle{std::make_shared<DutyCycle>(digitalSource)} {
|
||||
Init(fullRange, expectedZero);
|
||||
}
|
||||
|
||||
void DutyCycleEncoder::Init(double fullRange, double expectedZero) {
|
||||
m_simDevice = hal::SimDevice{"DutyCycle:DutyCycleEncoder",
|
||||
m_dutyCycle->GetSourceChannel()};
|
||||
|
||||
@@ -36,29 +36,11 @@ class DutyCycle : public wpi::Sendable, public wpi::SendableHelper<DutyCycle> {
|
||||
|
||||
public:
|
||||
/**
|
||||
* Constructs a DutyCycle input from a DigitalSource input.
|
||||
* Constructs a DutyCycle input from a smartio channel.
|
||||
*
|
||||
* <p> This class does not own the inputted source.
|
||||
*
|
||||
* @param source The DigitalSource to use.
|
||||
* @param source The channel 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);
|
||||
explicit DutyCycle(int source);
|
||||
|
||||
DutyCycle(DutyCycle&&) = default;
|
||||
DutyCycle& operator=(DutyCycle&&) = default;
|
||||
@@ -121,7 +103,7 @@ class DutyCycle : public wpi::Sendable, public wpi::SendableHelper<DutyCycle> {
|
||||
|
||||
private:
|
||||
void InitDutyCycle();
|
||||
std::shared_ptr<DigitalSource> m_source;
|
||||
int m_channel;
|
||||
hal::Handle<HAL_DutyCycleHandle, HAL_FreeDutyCycle> m_handle;
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
@@ -61,33 +61,6 @@ class DutyCycleEncoder : public wpi::Sendable,
|
||||
*/
|
||||
explicit DutyCycleEncoder(std::shared_ptr<DutyCycle> dutyCycle);
|
||||
|
||||
/**
|
||||
* Construct a new DutyCycleEncoder attached to a DigitalSource object.
|
||||
*
|
||||
* <p>This has a fullRange of 1 and an expectedZero of 0.
|
||||
*
|
||||
* @param digitalSource the digital source to attach to
|
||||
*/
|
||||
explicit DutyCycleEncoder(DigitalSource& digitalSource);
|
||||
|
||||
/**
|
||||
* Construct a new DutyCycleEncoder attached to a DigitalSource object.
|
||||
*
|
||||
* <p>This has a fullRange of 1 and an expectedZero of 0.
|
||||
*
|
||||
* @param digitalSource the digital source to attach to
|
||||
*/
|
||||
explicit DutyCycleEncoder(DigitalSource* digitalSource);
|
||||
|
||||
/**
|
||||
* Construct a new DutyCycleEncoder attached to a DigitalSource object.
|
||||
*
|
||||
* <p>This has a fullRange of 1 and an expectedZero of 0.
|
||||
*
|
||||
* @param digitalSource the digital source to attach to
|
||||
*/
|
||||
explicit DutyCycleEncoder(std::shared_ptr<DigitalSource> digitalSource);
|
||||
|
||||
/**
|
||||
* Construct a new DutyCycleEncoder on a specific channel.
|
||||
*
|
||||
@@ -125,36 +98,6 @@ class DutyCycleEncoder : public wpi::Sendable,
|
||||
DutyCycleEncoder(std::shared_ptr<DutyCycle> dutyCycle, double fullRange,
|
||||
double expectedZero);
|
||||
|
||||
/**
|
||||
* Construct a new DutyCycleEncoder attached to a DigitalSource object.
|
||||
*
|
||||
* @param digitalSource the digital source to attach to
|
||||
* @param fullRange the value to report at maximum travel
|
||||
* @param expectedZero the reading where you would expect a 0 from get()
|
||||
*/
|
||||
DutyCycleEncoder(DigitalSource& digitalSource, double fullRange,
|
||||
double expectedZero);
|
||||
|
||||
/**
|
||||
* Construct a new DutyCycleEncoder attached to a DigitalSource object.
|
||||
*
|
||||
* @param digitalSource the digital source to attach to
|
||||
* @param fullRange the value to report at maximum travel
|
||||
* @param expectedZero the reading where you would expect a 0 from get()
|
||||
*/
|
||||
DutyCycleEncoder(DigitalSource* digitalSource, double fullRange,
|
||||
double expectedZero);
|
||||
|
||||
/**
|
||||
* Construct a new DutyCycleEncoder attached to a DigitalSource object.
|
||||
*
|
||||
* @param digitalSource the digital source to attach to
|
||||
* @param fullRange the value to report at maximum travel
|
||||
* @param expectedZero the reading where you would expect a 0 from get()
|
||||
*/
|
||||
DutyCycleEncoder(std::shared_ptr<DigitalSource> digitalSource,
|
||||
double fullRange, double expectedZero);
|
||||
|
||||
~DutyCycleEncoder() override = default;
|
||||
|
||||
DutyCycleEncoder(DutyCycleEncoder&&) = default;
|
||||
|
||||
Reference in New Issue
Block a user