2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Relay.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
#include <hal/HALBase.h>
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/Ports.h>
|
|
|
|
|
#include <hal/Relay.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/raw_ostream.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SensorUtil.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
#include "frc/smartdashboard/SendableRegistry.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
Relay::Relay(int channel, Relay::Direction direction)
|
2015-06-29 02:43:44 -07:00
|
|
|
: m_channel(channel), m_direction(direction) {
|
2018-05-23 20:22:30 -07:00
|
|
|
if (!SensorUtil::CheckRelayChannel(m_channel)) {
|
2021-04-18 20:35:29 -07:00
|
|
|
throw FRC_MakeError(err::ChannelIndexOutOfRange,
|
|
|
|
|
"Relay Channel " + wpi::Twine{m_channel});
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_PortHandle portHandle = HAL_GetPort(channel);
|
2016-06-29 18:58:14 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
2016-06-29 18:58:14 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
m_forwardHandle = HAL_InitializeRelayPort(portHandle, true, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Relay Channel " + wpi::Twine{m_channel});
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Relay, m_channel + 1);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
2016-06-29 18:58:14 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
m_reverseHandle = HAL_InitializeRelayPort(portHandle, false, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Relay Channel " + wpi::Twine{m_channel});
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Relay, m_channel + 128);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
if (m_forwardHandle != HAL_kInvalidHandle) {
|
|
|
|
|
HAL_SetRelay(m_forwardHandle, false, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Relay Channel " + wpi::Twine{m_channel});
|
2016-06-29 18:58:14 -07:00
|
|
|
}
|
2016-07-09 00:24:26 -07:00
|
|
|
if (m_reverseHandle != HAL_kInvalidHandle) {
|
|
|
|
|
HAL_SetRelay(m_reverseHandle, false, &status);
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Relay Channel " + wpi::Twine{m_channel});
|
2016-06-29 18:58:14 -07:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2019-09-14 15:22:54 -05:00
|
|
|
SendableRegistry::GetInstance().AddLW(this, "Relay", m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Relay::~Relay() {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetRelay(m_forwardHandle, false, &status);
|
|
|
|
|
HAL_SetRelay(m_reverseHandle, false, &status);
|
2016-06-29 18:58:14 -07:00
|
|
|
// ignore errors, as we want to make sure a free happens.
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_forwardHandle != HAL_kInvalidHandle) {
|
|
|
|
|
HAL_FreeRelayPort(m_forwardHandle);
|
|
|
|
|
}
|
|
|
|
|
if (m_reverseHandle != HAL_kInvalidHandle) {
|
|
|
|
|
HAL_FreeRelayPort(m_reverseHandle);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Relay::Set(Relay::Value value) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
|
case kOff:
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetRelay(m_forwardHandle, false, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetRelay(m_reverseHandle, false, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kOn:
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetRelay(m_forwardHandle, true, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetRelay(m_reverseHandle, true, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kForward:
|
|
|
|
|
if (m_direction == kReverseOnly) {
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_ReportError(err::IncompatibleMode, "setting forward");
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetRelay(m_forwardHandle, true, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetRelay(m_reverseHandle, false, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kReverse:
|
|
|
|
|
if (m_direction == kForwardOnly) {
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_ReportError(err::IncompatibleMode, "setting reverse");
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetRelay(m_forwardHandle, false, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetRelay(m_reverseHandle, true, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Set");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
Relay::Value Relay::Get() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status;
|
|
|
|
|
|
2017-05-08 21:54:03 -07:00
|
|
|
if (m_direction == kForwardOnly) {
|
|
|
|
|
if (HAL_GetRelay(m_forwardHandle, &status)) {
|
|
|
|
|
return kOn;
|
|
|
|
|
} else {
|
|
|
|
|
return kOff;
|
|
|
|
|
}
|
|
|
|
|
} else if (m_direction == kReverseOnly) {
|
2016-07-09 00:24:26 -07:00
|
|
|
if (HAL_GetRelay(m_reverseHandle, &status)) {
|
2015-06-25 15:07:55 -04:00
|
|
|
return kOn;
|
|
|
|
|
} else {
|
2017-05-08 21:54:03 -07:00
|
|
|
return kOff;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
2017-05-08 21:54:03 -07:00
|
|
|
if (HAL_GetRelay(m_forwardHandle, &status)) {
|
|
|
|
|
if (HAL_GetRelay(m_reverseHandle, &status)) {
|
2015-06-25 15:07:55 -04:00
|
|
|
return kOn;
|
|
|
|
|
} else {
|
2017-05-08 21:54:03 -07:00
|
|
|
return kForward;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
2017-05-08 21:54:03 -07:00
|
|
|
if (HAL_GetRelay(m_reverseHandle, &status)) {
|
|
|
|
|
return kReverse;
|
|
|
|
|
} else {
|
|
|
|
|
return kOff;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Get");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int Relay::GetChannel() const {
|
|
|
|
|
return m_channel;
|
|
|
|
|
}
|
2015-07-09 03:24:31 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Relay::StopMotor() {
|
|
|
|
|
Set(kOff);
|
|
|
|
|
}
|
2015-07-09 03:24:31 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
void Relay::GetDescription(wpi::raw_ostream& desc) const {
|
2015-07-09 03:24:31 -07:00
|
|
|
desc << "Relay " << GetChannel();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void Relay::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Relay");
|
2018-07-28 14:04:46 -07:00
|
|
|
builder.SetActuator(true);
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSafeState([=]() { Set(kOff); });
|
|
|
|
|
builder.AddSmallStringProperty(
|
|
|
|
|
"Value",
|
2018-04-29 23:33:19 -07:00
|
|
|
[=](wpi::SmallVectorImpl<char>& buf) -> wpi::StringRef {
|
2017-12-04 23:28:33 -08:00
|
|
|
switch (Get()) {
|
|
|
|
|
case kOn:
|
|
|
|
|
return "On";
|
|
|
|
|
case kForward:
|
|
|
|
|
return "Forward";
|
|
|
|
|
case kReverse:
|
|
|
|
|
return "Reverse";
|
|
|
|
|
default:
|
|
|
|
|
return "Off";
|
|
|
|
|
}
|
|
|
|
|
},
|
2018-04-29 23:33:19 -07:00
|
|
|
[=](wpi::StringRef value) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (value == "Off") {
|
2017-12-04 23:28:33 -08:00
|
|
|
Set(kOff);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else if (value == "Forward") {
|
2017-12-04 23:28:33 -08:00
|
|
|
Set(kForward);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else if (value == "Reverse") {
|
2017-12-04 23:28:33 -08:00
|
|
|
Set(kReverse);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else if (value == "On") {
|
2017-12-04 23:28:33 -08:00
|
|
|
Set(kOn);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
});
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|