2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Relay.h"
|
|
|
|
|
|
2014-01-06 10:12:21 -05:00
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Resource.h"
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include "LiveWindow/LiveWindow.h"
|
2014-08-08 17:05:49 -04:00
|
|
|
#include "HAL/HAL.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-30 15:01:20 -04:00
|
|
|
#include <sstream>
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
// Allocate each direction separately.
|
2015-07-29 16:48:04 -04:00
|
|
|
static std::unique_ptr<Resource> relayChannels;
|
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.
|
|
|
|
|
* @param channel The channel number (0-3).
|
|
|
|
|
* @param direction The direction that the Relay object will control.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-29 02:43:44 -07:00
|
|
|
Relay::Relay(uint32_t channel, Relay::Direction direction)
|
|
|
|
|
: m_channel(channel), m_direction(direction) {
|
2015-06-30 15:01:20 -04:00
|
|
|
std::stringstream buf;
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
Resource::CreateResourceObject(relayChannels,
|
2015-06-25 15:07:55 -04:00
|
|
|
dio_kNumSystems * kRelayChannels * 2);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
2015-06-30 15:01:20 -04:00
|
|
|
buf << "Forward Relay " << m_channel;
|
|
|
|
|
if (relayChannels->Allocate(m_channel * 2, buf.str()) == ~0ul) {
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
CloneError(*relayChannels);
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Relay, m_channel);
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
2015-06-30 15:01:20 -04:00
|
|
|
buf << "Reverse Relay " << m_channel;
|
|
|
|
|
if (relayChannels->Allocate(m_channel * 2 + 1, buf.str()) == ~0ul) {
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
CloneError(*relayChannels);
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Relay, m_channel + 128);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
setRelayForward(m_relay_ports[m_channel], false, &status);
|
|
|
|
|
setRelayReverse(m_relay_ports[m_channel], false, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
|
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.
|
|
|
|
|
* 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;
|
|
|
|
|
setRelayForward(m_relay_ports[m_channel], false, &status);
|
|
|
|
|
setRelayReverse(m_relay_ports[m_channel], false, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
|
|
|
|
relayChannels->Free(m_channel * 2);
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
|
|
|
|
relayChannels->Free(m_channel * 2 + 1);
|
|
|
|
|
}
|
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:
|
2015-06-25 15:07:55 -04: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
|
|
|
|
|
* 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) {
|
|
|
|
|
setRelayForward(m_relay_ports[m_channel], false, &status);
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
|
|
|
|
setRelayReverse(m_relay_ports[m_channel], false, &status);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kOn:
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
|
|
|
|
setRelayForward(m_relay_ports[m_channel], true, &status);
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
|
|
|
|
setRelayReverse(m_relay_ports[m_channel], true, &status);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kForward:
|
|
|
|
|
if (m_direction == kReverseOnly) {
|
|
|
|
|
wpi_setWPIError(IncompatibleMode);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kForwardOnly) {
|
|
|
|
|
setRelayForward(m_relay_ports[m_channel], true, &status);
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections) {
|
|
|
|
|
setRelayReverse(m_relay_ports[m_channel], false, &status);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case kReverse:
|
|
|
|
|
if (m_direction == kForwardOnly) {
|
|
|
|
|
wpi_setWPIError(IncompatibleMode);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections) {
|
|
|
|
|
setRelayForward(m_relay_ports[m_channel], false, &status);
|
|
|
|
|
}
|
|
|
|
|
if (m_direction == kBothDirections || m_direction == kReverseOnly) {
|
|
|
|
|
setRelayReverse(m_relay_ports[m_channel], true, &status);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(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;
|
|
|
|
|
|
|
|
|
|
if (getRelayForward(m_relay_ports[m_channel], &status)) {
|
|
|
|
|
if (getRelayReverse(m_relay_ports[m_channel], &status)) {
|
|
|
|
|
return kOn;
|
|
|
|
|
} else {
|
|
|
|
|
if (m_direction == kForwardOnly) {
|
|
|
|
|
return kOn;
|
|
|
|
|
} else {
|
|
|
|
|
return kForward;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (getRelayReverse(m_relay_ports[m_channel], &status)) {
|
|
|
|
|
if (m_direction == kReverseOnly) {
|
|
|
|
|
return kOn;
|
|
|
|
|
} else {
|
|
|
|
|
return kReverse;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return kOff;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Relay::ValueChanged(std::shared_ptr<ITable> source, const std::string &key,
|
2015-06-25 15:07:55 -04:00
|
|
|
EntryValue value, bool isNew) {
|
|
|
|
|
std::string *val = (std::string *)value.ptr;
|
|
|
|
|
if (*val == "Off")
|
|
|
|
|
Set(kOff);
|
|
|
|
|
else if (*val == "On")
|
|
|
|
|
Set(kOn);
|
|
|
|
|
else if (*val == "Forward")
|
|
|
|
|
Set(kForward);
|
|
|
|
|
else if (*val == "Reverse")
|
|
|
|
|
Set(kReverse);
|
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; }
|