2014-07-17 11:01:14 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Relay.h"
|
|
|
|
|
|
|
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
|
|
|
|
|
|
/**
|
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.
|
|
|
|
|
* @param channel The channel number (0-3).
|
|
|
|
|
* @param direction The direction that the Relay object will control.
|
2014-07-17 11:01:14 -07:00
|
|
|
*/
|
2015-06-29 02:43:44 -07:00
|
|
|
Relay::Relay(uint32_t channel, Relay::Direction direction)
|
|
|
|
|
: m_channel (channel)
|
|
|
|
|
, m_direction (direction)
|
2014-07-17 11:01:14 -07:00
|
|
|
{
|
|
|
|
|
char buf[64];
|
|
|
|
|
if (!SensorBase::CheckRelayChannel(m_channel))
|
|
|
|
|
{
|
|
|
|
|
snprintf(buf, 64, "Relay Channel %d", m_channel);
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sprintf(buf, "relay/%d", m_channel);
|
|
|
|
|
impl = new SimContinuousOutput(buf); // TODO: Allow two different relays (targetting the different halves of a relay) to be combined to control one motor.
|
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);
|
2014-07-17 11:01:14 -07:00
|
|
|
go_pos = go_neg = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Free the resource associated with a relay.
|
|
|
|
|
* The relay channels are set to free and the relay output is turned off.
|
|
|
|
|
*/
|
|
|
|
|
Relay::~Relay()
|
|
|
|
|
{
|
|
|
|
|
impl->Set(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 Relay::Set(Relay::Value value)
|
|
|
|
|
{
|
|
|
|
|
switch (value)
|
|
|
|
|
{
|
|
|
|
|
case kOff:
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly)
|
|
|
|
|
{
|
|
|
|
|
go_pos = false;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly)
|
|
|
|
|
{
|
|
|
|
|
go_neg = false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kOn:
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly)
|
|
|
|
|
{
|
|
|
|
|
go_pos = true;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly)
|
|
|
|
|
{
|
|
|
|
|
go_neg = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kForward:
|
|
|
|
|
if (m_direction == kReverseOnly)
|
|
|
|
|
{
|
|
|
|
|
wpi_setWPIError(IncompatibleMode);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly)
|
|
|
|
|
{
|
|
|
|
|
go_pos = true;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections)
|
|
|
|
|
{
|
|
|
|
|
go_neg = false;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kReverse:
|
|
|
|
|
if (m_direction == kForwardOnly)
|
|
|
|
|
{
|
|
|
|
|
wpi_setWPIError(IncompatibleMode);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections)
|
|
|
|
|
{
|
|
|
|
|
go_pos = false;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly)
|
|
|
|
|
{
|
|
|
|
|
go_neg = true;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
impl->Set((go_pos ? 1 : 0) + (go_neg ? -1 : 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2015-06-19 17:23:54 -07:00
|
|
|
Relay::Value Relay::Get() const {
|
2014-07-17 11:01:14 -07:00
|
|
|
// TODO: Don't assume that the go_pos and go_neg fields are correct?
|
|
|
|
|
if ((go_pos || m_direction == kReverseOnly) && (go_neg || m_direction == kForwardOnly)) {
|
|
|
|
|
return kOn;
|
|
|
|
|
} else if (go_pos) {
|
|
|
|
|
return kForward;
|
|
|
|
|
} else if (go_neg) {
|
|
|
|
|
return kReverse;
|
|
|
|
|
} else {
|
|
|
|
|
return kOff;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Relay::ValueChanged(std::shared_ptr<ITable> source, const std::string& key, EntryValue value, bool isNew) {
|
2014-07-17 11:01:14 -07:00
|
|
|
std::string *val = (std::string *) value.ptr;
|
|
|
|
|
if (*val == "Off") Set(kOff);
|
|
|
|
|
else if (*val == "Forward") Set(kForward);
|
|
|
|
|
else if (*val == "Reverse") Set(kReverse);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Relay::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if(m_table != nullptr){
|
2014-07-17 11:01:14 -07: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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Relay::StartLiveWindowMode() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if(m_table != nullptr){
|
2014-07-17 11:01:14 -07:00
|
|
|
m_table->AddTableListener("Value", this, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Relay::StopLiveWindowMode() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if(m_table != nullptr){
|
2014-07-17 11:01:14 -07:00
|
|
|
m_table->RemoveTableListener(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
std::string Relay::GetSmartDashboardType() const {
|
2014-07-17 11:01:14 -07:00
|
|
|
return "Relay";
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Relay::InitTable(std::shared_ptr<ITable> subTable) {
|
2014-07-17 11:01:14 -07:00
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
std::shared_ptr<ITable> Relay::GetTable() const {
|
2014-07-17 11:01:14 -07:00
|
|
|
return m_table;
|
|
|
|
|
}
|