2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* 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. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Relay.h"
|
|
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
#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
|
|
|
|
2015-07-09 03:24:31 -07:00
|
|
|
#include "MotorSafetyHelper.h"
|
2017-12-04 23:28:33 -08:00
|
|
|
#include "SensorBase.h"
|
|
|
|
|
#include "SmartDashboard/SendableBuilder.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2015-06-29 02:43:44 -07:00
|
|
|
* 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.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @param channel The channel number (0-3).
|
2015-06-29 02:43:44 -07:00
|
|
|
* @param direction The direction that the Relay object will control.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
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) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!SensorBase::CheckRelayChannel(m_channel)) {
|
2017-12-01 21:50:24 -08:00
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange,
|
2018-04-29 23:33:19 -07:00
|
|
|
"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);
|
2016-06-29 18:58:14 -07:00
|
|
|
if (status != 0) {
|
2016-08-12 13:45:28 -07:00
|
|
|
wpi_setErrorWithContextRange(status, 0, HAL_GetNumRelayChannels(),
|
|
|
|
|
channel, HAL_GetErrorMessage(status));
|
2016-07-09 00:24:26 -07:00
|
|
|
m_forwardHandle = HAL_kInvalidHandle;
|
|
|
|
|
m_reverseHandle = HAL_kInvalidHandle;
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Relay, m_channel);
|
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);
|
2016-06-29 18:58:14 -07:00
|
|
|
if (status != 0) {
|
2016-08-12 13:45:28 -07:00
|
|
|
wpi_setErrorWithContextRange(status, 0, HAL_GetNumRelayChannels(),
|
|
|
|
|
channel, HAL_GetErrorMessage(status));
|
2016-07-09 00:24:26 -07:00
|
|
|
m_forwardHandle = HAL_kInvalidHandle;
|
|
|
|
|
m_reverseHandle = HAL_kInvalidHandle;
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2016-06-29 18:58:14 -07:00
|
|
|
if (status != 0) {
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
m_forwardHandle = HAL_kInvalidHandle;
|
|
|
|
|
m_reverseHandle = HAL_kInvalidHandle;
|
2016-06-29 18:58:14 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-09 00:24:26 -07:00
|
|
|
if (m_reverseHandle != HAL_kInvalidHandle) {
|
|
|
|
|
HAL_SetRelay(m_reverseHandle, false, &status);
|
2016-06-29 18:58:14 -07:00
|
|
|
if (status != 0) {
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
m_forwardHandle = HAL_kInvalidHandle;
|
|
|
|
|
m_reverseHandle = HAL_kInvalidHandle;
|
2016-06-29 18:58:14 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2015-07-09 03:24:31 -07:00
|
|
|
m_safetyHelper = std::make_unique<MotorSafetyHelper>(this);
|
|
|
|
|
m_safetyHelper->SetSafetyEnabled(false);
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
SetName("Relay", m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Free the resource associated with a relay.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* The relay channels are set to free and the relay output is turned off.
|
|
|
|
|
*/
|
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.
|
2016-07-09 00:24:26 -07: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the relay state.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Valid values depend on which directions of the relay are controlled by the
|
|
|
|
|
* object.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* When set to kBothDirections, the relay can be any of the four states:
|
2016-05-20 17:30:37 -07:00
|
|
|
* 0v-0v, 0v-12v, 12v-0v, 12v-12v
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* When set to kForwardOnly or kReverseOnly, you can specify the constant for
|
2016-05-20 17:30:37 -07:00
|
|
|
* the direction or you can simply specify kOff and kOn. Using only kOff and
|
|
|
|
|
* kOn is recommended.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param value The state to set the relay.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Relay::Set(Relay::Value value) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
wpi_setWPIError(IncompatibleMode);
|
|
|
|
|
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) {
|
|
|
|
|
wpi_setWPIError(IncompatibleMode);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the Relay State
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* Gets the current state of the relay.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not
|
2017-11-16 00:33:51 -08:00
|
|
|
* kForward/kReverse (per the recommendation in Set).
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The current state of the relay as a Relay::Value
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int Relay::GetChannel() const { return m_channel; }
|
2015-07-09 03:24:31 -07:00
|
|
|
|
|
|
|
|
/**
|
2017-11-16 00:33:51 -08:00
|
|
|
* Set the expiration time for the Relay object.
|
|
|
|
|
*
|
2015-07-09 03:24:31 -07:00
|
|
|
* @param timeout The timeout (in seconds) for this relay object
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void Relay::SetExpiration(double timeout) {
|
2015-07-09 03:24:31 -07:00
|
|
|
m_safetyHelper->SetExpiration(timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the expiration time for the relay object.
|
2017-11-16 00:33:51 -08:00
|
|
|
*
|
2015-07-09 03:24:31 -07:00
|
|
|
* @return The expiration time value.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double Relay::GetExpiration() const { return m_safetyHelper->GetExpiration(); }
|
2015-07-09 03:24:31 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the relay object is currently alive or stopped due to a timeout.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* @return a bool value that is true if the motor has NOT timed out and should
|
|
|
|
|
* still be running.
|
2015-07-09 03:24:31 -07:00
|
|
|
*/
|
|
|
|
|
bool Relay::IsAlive() const { return m_safetyHelper->IsAlive(); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop the motor associated with this PWM object.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-07-09 03:24:31 -07:00
|
|
|
* This is called by the MotorSafetyHelper object when it has a timeout for this
|
|
|
|
|
* relay and needs to stop it from running.
|
|
|
|
|
*/
|
|
|
|
|
void Relay::StopMotor() { Set(kOff); }
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Enable/disable motor safety for this device.
|
|
|
|
|
*
|
2015-07-09 03:24:31 -07:00
|
|
|
* Turn on and off the motor safety option for this relay object.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-07-09 03:24:31 -07:00
|
|
|
* @param enabled True if motor safety is enforced for this object
|
|
|
|
|
*/
|
|
|
|
|
void Relay::SetSafetyEnabled(bool enabled) {
|
|
|
|
|
m_safetyHelper->SetSafetyEnabled(enabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Check if motor safety is enabled for this object.
|
|
|
|
|
*
|
2015-07-09 03:24:31 -07:00
|
|
|
* @returns True if motor safety is enforced for this object
|
|
|
|
|
*/
|
|
|
|
|
bool Relay::IsSafetyEnabled() const {
|
|
|
|
|
return m_safetyHelper->IsSafetyEnabled();
|
|
|
|
|
}
|
|
|
|
|
|
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");
|
|
|
|
|
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) {
|
2017-12-04 23:28:33 -08:00
|
|
|
if (value == "Off")
|
|
|
|
|
Set(kOff);
|
|
|
|
|
else if (value == "Forward")
|
|
|
|
|
Set(kForward);
|
|
|
|
|
else if (value == "Reverse")
|
|
|
|
|
Set(kReverse);
|
|
|
|
|
else if (value == "On")
|
|
|
|
|
Set(kOn);
|
|
|
|
|
});
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|