[hal, wpilib] Remove relay (#7695)

This commit is contained in:
Thad House
2025-01-16 23:20:07 -08:00
committed by GitHub
parent d9f8fded09
commit 5017393b3a
54 changed files with 1 additions and 3115 deletions

View File

@@ -1,201 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/Relay.h"
#include <string>
#include <fmt/format.h>
#include <hal/FRCUsageReporting.h>
#include <hal/HALBase.h>
#include <hal/Ports.h>
#include <hal/Relay.h>
#include <wpi/StackTrace.h>
#include <wpi/sendable/SendableBuilder.h>
#include <wpi/sendable/SendableRegistry.h>
#include "frc/Errors.h"
#include "frc/SensorUtil.h"
using namespace frc;
Relay::Relay(int channel, Relay::Direction direction)
: m_channel(channel), m_direction(direction) {
if (!SensorUtil::CheckRelayChannel(m_channel)) {
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", m_channel);
return;
}
HAL_PortHandle portHandle = HAL_GetPort(channel);
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
int32_t status = 0;
std::string stackTrace = wpi::GetStackTrace(1);
m_forwardHandle =
HAL_InitializeRelayPort(portHandle, true, stackTrace.c_str(), &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
HAL_Report(HALUsageReporting::kResourceType_Relay, m_channel + 1);
}
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
int32_t status = 0;
std::string stackTrace = wpi::GetStackTrace(1);
m_reverseHandle =
HAL_InitializeRelayPort(portHandle, false, stackTrace.c_str(), &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
HAL_Report(HALUsageReporting::kResourceType_Relay, m_channel + 128);
}
int32_t status = 0;
if (m_forwardHandle != HAL_kInvalidHandle) {
HAL_SetRelay(m_forwardHandle, false, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
}
if (m_reverseHandle != HAL_kInvalidHandle) {
HAL_SetRelay(m_reverseHandle, false, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
}
wpi::SendableRegistry::AddLW(this, "Relay", m_channel);
}
Relay::~Relay() {
int32_t status = 0;
if (m_forwardHandle != HAL_kInvalidHandle) {
HAL_SetRelay(m_forwardHandle, false, &status);
}
if (m_reverseHandle != HAL_kInvalidHandle) {
HAL_SetRelay(m_reverseHandle, false, &status);
}
}
void Relay::Set(Relay::Value value) {
int32_t status = 0;
switch (value) {
case kOff:
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
HAL_SetRelay(m_forwardHandle, false, &status);
}
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
HAL_SetRelay(m_reverseHandle, false, &status);
}
break;
case kOn:
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
HAL_SetRelay(m_forwardHandle, true, &status);
}
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
HAL_SetRelay(m_reverseHandle, true, &status);
}
break;
case kForward:
if (m_direction == kReverseOnly) {
FRC_ReportError(err::IncompatibleMode, "channel {} setting {}",
m_channel, "forward");
break;
}
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
HAL_SetRelay(m_forwardHandle, true, &status);
}
if (m_direction == kBothDirections) {
HAL_SetRelay(m_reverseHandle, false, &status);
}
break;
case kReverse:
if (m_direction == kForwardOnly) {
FRC_ReportError(err::IncompatibleMode, "channel {} setting {}",
m_channel, "reverse");
break;
}
if (m_direction == kBothDirections) {
HAL_SetRelay(m_forwardHandle, false, &status);
}
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
HAL_SetRelay(m_reverseHandle, true, &status);
}
break;
}
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
}
Relay::Value Relay::Get() const {
Relay::Value value = kOff;
int32_t status = 0;
if (m_direction == kForwardOnly) {
if (HAL_GetRelay(m_forwardHandle, &status)) {
value = kOn;
} else {
value = kOff;
}
} else if (m_direction == kReverseOnly) {
if (HAL_GetRelay(m_reverseHandle, &status)) {
value = kOn;
} else {
value = kOff;
}
} else {
if (HAL_GetRelay(m_forwardHandle, &status)) {
if (HAL_GetRelay(m_reverseHandle, &status)) {
value = kOn;
} else {
value = kForward;
}
} else {
if (HAL_GetRelay(m_reverseHandle, &status)) {
value = kReverse;
} else {
value = kOff;
}
}
}
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
return value;
}
int Relay::GetChannel() const {
return m_channel;
}
void Relay::StopMotor() {
Set(kOff);
}
std::string Relay::GetDescription() const {
return fmt::format("Relay {}", GetChannel());
}
void Relay::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("Relay");
builder.SetActuator(true);
builder.SetSafeState([=, this] { Set(kOff); });
builder.AddSmallStringProperty(
"Value",
[=, this](wpi::SmallVectorImpl<char>& buf) -> std::string_view {
switch (Get()) {
case kOn:
return "On";
case kForward:
return "Forward";
case kReverse:
return "Reverse";
default:
return "Off";
}
},
[=, this](std::string_view value) {
if (value == "Off") {
Set(kOff);
} else if (value == "Forward") {
Set(kForward);
} else if (value == "Reverse") {
Set(kReverse);
} else if (value == "On") {
Set(kOn);
}
});
}

View File

@@ -9,7 +9,6 @@
#include <hal/DIO.h>
#include <hal/PWM.h>
#include <hal/Ports.h>
#include <hal/Relay.h>
using namespace frc;
@@ -25,10 +24,6 @@ bool SensorUtil::CheckDigitalChannel(int channel) {
return HAL_CheckDIOChannel(channel);
}
bool SensorUtil::CheckRelayChannel(int channel) {
return HAL_CheckRelayChannel(channel);
}
bool SensorUtil::CheckPWMChannel(int channel) {
return HAL_CheckPWMChannel(channel);
}
@@ -56,7 +51,3 @@ int SensorUtil::GetNumAnalogOuputs() {
int SensorUtil::GetNumPwmChannels() {
return HAL_GetNumPWMChannels();
}
int SensorUtil::GetNumRelayChannels() {
return HAL_GetNumRelayHeaders();
}

View File

@@ -1,90 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/simulation/RelaySim.h"
#include <memory>
#include <hal/simulation/RelayData.h>
#include "frc/Relay.h"
using namespace frc;
using namespace frc::sim;
RelaySim::RelaySim(const Relay& relay) : m_index{relay.GetChannel()} {}
RelaySim::RelaySim(int channel) : m_index{channel} {}
std::unique_ptr<CallbackStore> RelaySim::RegisterInitializedForwardCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelRelayInitializedForwardCallback);
store->SetUid(HALSIM_RegisterRelayInitializedForwardCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
return store;
}
bool RelaySim::GetInitializedForward() const {
return HALSIM_GetRelayInitializedForward(m_index);
}
void RelaySim::SetInitializedForward(bool initializedForward) {
HALSIM_SetRelayInitializedForward(m_index, initializedForward);
}
std::unique_ptr<CallbackStore> RelaySim::RegisterInitializedReverseCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelRelayInitializedReverseCallback);
store->SetUid(HALSIM_RegisterRelayInitializedReverseCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
return store;
}
bool RelaySim::GetInitializedReverse() const {
return HALSIM_GetRelayInitializedReverse(m_index);
}
void RelaySim::SetInitializedReverse(bool initializedReverse) {
HALSIM_SetRelayInitializedReverse(m_index, initializedReverse);
}
std::unique_ptr<CallbackStore> RelaySim::RegisterForwardCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelRelayForwardCallback);
store->SetUid(HALSIM_RegisterRelayForwardCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
return store;
}
bool RelaySim::GetForward() const {
return HALSIM_GetRelayForward(m_index);
}
void RelaySim::SetForward(bool forward) {
HALSIM_SetRelayForward(m_index, forward);
}
std::unique_ptr<CallbackStore> RelaySim::RegisterReverseCallback(
NotifyCallback callback, bool initialNotify) {
auto store = std::make_unique<CallbackStore>(
m_index, -1, callback, &HALSIM_CancelRelayReverseCallback);
store->SetUid(HALSIM_RegisterRelayReverseCallback(
m_index, &CallbackStoreThunk, store.get(), initialNotify));
return store;
}
bool RelaySim::GetReverse() const {
return HALSIM_GetRelayReverse(m_index);
}
void RelaySim::SetReverse(bool reverse) {
HALSIM_SetRelayReverse(m_index, reverse);
}
void RelaySim::ResetData() {
HALSIM_ResetRelayData(m_index);
}

View File

@@ -1,128 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <memory>
#include <string>
#include <hal/Relay.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
#include "frc/MotorSafety.h"
namespace frc {
/**
* Class for Spike style relay outputs.
*
* Relays are intended to be connected to spikes or similar relays. The relay
* channels controls a pair of pins that are either both off, one on, the other
* on, or both on. This translates into two spike outputs at 0v, one at 12v and
* one at 0v, one at 0v and the other at 12v, or two spike outputs at 12V. This
* allows off, full forward, or full reverse control of motors without variable
* speed. It also allows the two channels (forward and reverse) to be used
* independently for something that does not care about voltage polarity (like
* a solenoid).
*/
class Relay : public MotorSafety,
public wpi::Sendable,
public wpi::SendableHelper<Relay> {
public:
/**
* The state to drive a Relay to.
*/
enum Value {
/// Off.
kOff,
/// On.
kOn,
/// Forward.
kForward,
/// Reverse.
kReverse
};
/**
* The Direction(s) that a relay is configured to operate in.
*/
enum Direction {
/// Both directions are valid.
kBothDirections,
/// Only forward is valid.
kForwardOnly,
/// Only reverse is valid.
kReverseOnly
};
/**
* Relay constructor given a channel.
*
* This code initializes the relay and reserves all resources that need to be
* locked. Initially the relay is set to both lines at 0v.
*
* @param channel The channel number (0-3).
* @param direction The direction that the Relay object will control.
*/
explicit Relay(int channel, Direction direction = kBothDirections);
Relay(Relay&&) = default;
Relay& operator=(Relay&&) = default;
/**
* Free the resource associated with a relay.
*
* The relay channels are set to free and the relay output is turned off.
*/
~Relay() override;
/**
* Set the relay state.
*
* Valid values depend on which directions of the relay are controlled by the
* object.
*
* When set to kBothDirections, the relay can be any of the four states:
* 0v-0v, 0v-12v, 12v-0v, 12v-12v
*
* When set to kForwardOnly or kReverseOnly, you can specify the constant for
* the direction or you can simply specify kOff and kOn. Using only kOff and
* kOn is recommended.
*
* @param value The state to set the relay.
*/
void Set(Value value);
/**
* Get the Relay State
*
* Gets the current state of the relay.
*
* When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not
* kForward/kReverse (per the recommendation in Set).
*
* @return The current state of the relay as a Relay::Value
*/
Value Get() const;
int GetChannel() const;
// MotorSafety interface
void StopMotor() override;
std::string GetDescription() const override;
void InitSendable(wpi::SendableBuilder& builder) override;
private:
int m_channel;
Direction m_direction;
hal::Handle<HAL_RelayHandle, HAL_FreeRelayPort> m_forwardHandle;
hal::Handle<HAL_RelayHandle, HAL_FreeRelayPort> m_reverseHandle;
};
} // namespace frc

View File

@@ -38,16 +38,6 @@ class SensorUtil final {
*/
static bool CheckDigitalChannel(int channel);
/**
* Check that the relay channel number is valid.
*
* Verify that the channel number is one of the legal channel numbers. Channel
* numbers are 0-based.
*
* @return Relay channel is valid
*/
static bool CheckRelayChannel(int channel);
/**
* Check that the digital channel number is valid.
*
@@ -82,7 +72,6 @@ class SensorUtil final {
static int GetNumAnalogInputs();
static int GetNumAnalogOuputs();
static int GetNumPwmChannels();
static int GetNumRelayChannels();
};
} // namespace frc

View File

@@ -1,145 +0,0 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <memory>
#include "frc/simulation/CallbackStore.h"
namespace frc {
class Relay;
namespace sim {
/**
* Class to control a simulated relay.
*/
class RelaySim {
public:
/**
* Constructs from a Relay object.
*
* @param relay Relay to simulate
*/
explicit RelaySim(const Relay& relay);
/**
* Constructs from a relay channel number.
*
* @param channel Channel number
*/
explicit RelaySim(int channel);
/**
* Register a callback to be run when the forward direction is initialized.
*
* @param callback the callback
* @param initialNotify whether to run the callback with the initial state
* @return the CallbackStore object associated with this callback
*/
[[nodiscard]]
std::unique_ptr<CallbackStore> RegisterInitializedForwardCallback(
NotifyCallback callback, bool initialNotify);
/**
* Check whether the forward direction has been initialized.
*
* @return true if initialized
*/
bool GetInitializedForward() const;
/**
* Define whether the forward direction has been initialized.
*
* @param initializedForward whether this object is initialized
*/
void SetInitializedForward(bool initializedForward);
/**
* Register a callback to be run when the reverse direction is initialized.
*
* @param callback the callback
* @param initialNotify whether to run the callback with the initial state
* @return the CallbackStore object associated with this callback
*/
[[nodiscard]]
std::unique_ptr<CallbackStore> RegisterInitializedReverseCallback(
NotifyCallback callback, bool initialNotify);
/**
* Check whether the reverse direction has been initialized.
*
* @return true if initialized
*/
bool GetInitializedReverse() const;
/**
* Define whether the reverse direction has been initialized.
*
* @param initializedReverse whether this object is initialized
*/
void SetInitializedReverse(bool initializedReverse);
/**
* Register a callback to be run when the forward direction changes state.
*
* @param callback the callback
* @param initialNotify whether to run the callback with the initial state
* @return the CallbackStore object associated with this callback
*/
[[nodiscard]]
std::unique_ptr<CallbackStore> RegisterForwardCallback(
NotifyCallback callback, bool initialNotify);
/**
* Check whether the forward direction is active.
*
* @return true if active
*/
bool GetForward() const;
/**
* Set whether the forward direction is active.
*
* @param forward true to make active
*/
void SetForward(bool forward);
/**
* Register a callback to be run when the reverse direction changes state.
*
* @param callback the callback
* @param initialNotify whether to run the callback with the initial state
* @return the CallbackStore object associated with this callback
*/
[[nodiscard]]
std::unique_ptr<CallbackStore> RegisterReverseCallback(
NotifyCallback callback, bool initialNotify);
/**
* Check whether the reverse direction is active.
*
* @return true if active
*/
bool GetReverse() const;
/**
* Set whether the reverse direction is active.
*
* @param reverse true to make active
*/
void SetReverse(bool reverse);
/**
* Reset all simulation data.
*/
void ResetData();
private:
int m_index;
};
} // namespace sim
} // namespace frc