2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-01-01 01:05:57 -07:00
|
|
|
/* Copyright (c) FIRST 2008-2017. 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
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-12-21 21:55:31 -08:00
|
|
|
#include "HAL/Relay.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Relay.h"
|
|
|
|
|
|
2016-05-22 21:41:22 -07:00
|
|
|
#include "HAL/HAL.h"
|
2016-12-21 21:55:31 -08:00
|
|
|
#include "HAL/Ports.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "LiveWindow/LiveWindow.h"
|
2015-07-09 03:24:31 -07:00
|
|
|
#include "MotorSafetyHelper.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "WPIErrors.h"
|
2017-05-15 23:10:40 -07:00
|
|
|
#include "llvm/SmallString.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
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) {
|
2017-05-15 23:10:40 -07:00
|
|
|
llvm::SmallString<128> str;
|
|
|
|
|
llvm::raw_svector_ostream buf(str);
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!SensorBase::CheckRelayChannel(m_channel)) {
|
2015-06-30 15:01:20 -04:00
|
|
|
buf << "Relay Channel " << m_channel;
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
|
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);
|
|
|
|
|
|
Revert changes preventing old user code from compiling.
I'm not 100% sure whether we want these, but they are a quick
find and replace to do.
Basically, there are two primary things that we have done
this summer that break existing user code:
-Changing GetInstance() calls to return references instead
of pointers. This forces users to change from doing something
like LiveWindow::GetInstance()->AddSensor() to LiveWindow::GetInstance().AddSensor().
-Making PIDGet() and related calls const, forcing users to change
the function signatures wherever they override them.
The GetInstance() calls don't really matter to me either way,
especially since there are no real ownership issues going on there,
unlike the rest of the smart pointer-related changes.
For the const stuff, it is certainly more correct to mandate that
user PIDGet() functions be const and the such, but at the same time,
I'm not sure that there is any strong need for it, and the errors
generated are not the most helpful. While this wouldn't necessarily
be an issue for more experienced teams or completely new teams (who
don't have any old code to be reusing), it may cause issues for more
average teams who aren't familiar with the intricacies of C++ anything.
Change-Id: I6e7007982069292ea70e6d0fc8ca40203340df1b
2015-07-24 19:19:40 -04:00
|
|
|
LiveWindow::GetInstance()->AddActuator("Relay", 1, m_channel, this);
|
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);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
if (m_table != nullptr) m_table->RemoveTableListener(this);
|
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
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the expiration time for the Relay object
|
|
|
|
|
* @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.
|
|
|
|
|
* @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();
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 23:10:40 -07:00
|
|
|
void Relay::GetDescription(llvm::raw_ostream& desc) const {
|
2015-07-09 03:24:31 -07:00
|
|
|
desc << "Relay " << GetChannel();
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
void Relay::ValueChanged(ITable* source, llvm::StringRef key,
|
|
|
|
|
std::shared_ptr<nt::Value> value, bool isNew) {
|
|
|
|
|
if (!value->IsString()) return;
|
2016-05-20 17:30:37 -07:00
|
|
|
if (value->GetString() == "Off")
|
|
|
|
|
Set(kOff);
|
|
|
|
|
else if (value->GetString() == "Forward")
|
|
|
|
|
Set(kForward);
|
|
|
|
|
else if (value->GetString() == "Reverse")
|
|
|
|
|
Set(kReverse);
|
|
|
|
|
else if (value->GetString() == "On")
|
|
|
|
|
Set(kOn);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Relay::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (Get() == kOn) {
|
|
|
|
|
m_table->PutString("Value", "On");
|
|
|
|
|
} else if (Get() == kForward) {
|
|
|
|
|
m_table->PutString("Value", "Forward");
|
|
|
|
|
} else if (Get() == kReverse) {
|
|
|
|
|
m_table->PutString("Value", "Reverse");
|
|
|
|
|
} else {
|
|
|
|
|
m_table->PutString("Value", "Off");
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Relay::StartLiveWindowMode() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->AddTableListener("Value", this, true);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Relay::StopLiveWindowMode() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->RemoveTableListener(this);
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string Relay::GetSmartDashboardType() const { return "Relay"; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Relay::InitTable(std::shared_ptr<ITable> subTable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
std::shared_ptr<ITable> Relay::GetTable() const { return m_table; }
|