2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-01-01 01:05:57 -07:00
|
|
|
/* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* 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/Solenoid.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Solenoid.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
|
2016-05-25 22:38:11 -07:00
|
|
|
#include "HAL/HAL.h"
|
2016-12-21 21:55:31 -08:00
|
|
|
#include "HAL/Ports.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "LiveWindow/LiveWindow.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "WPIErrors.h"
|
2017-05-15 23:10:40 -07:00
|
|
|
#include "llvm/SmallString.h"
|
|
|
|
|
#include "llvm/raw_ostream.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
|
|
|
* Constructor using the default PCM ID (0).
|
|
|
|
|
*
|
|
|
|
|
* @param channel The channel on the PCM to control (0..7).
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
Solenoid::Solenoid(int channel)
|
2015-06-29 02:43:44 -07:00
|
|
|
: Solenoid(GetDefaultSolenoidModule(), channel) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param moduleNumber The CAN ID of the PCM the solenoid is attached to
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param channel The channel on the PCM to control (0..7).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
Solenoid::Solenoid(int moduleNumber, int channel)
|
2015-06-29 02:43:44 -07:00
|
|
|
: SolenoidBase(moduleNumber), m_channel(channel) {
|
2017-05-15 23:10:40 -07:00
|
|
|
llvm::SmallString<32> str;
|
|
|
|
|
llvm::raw_svector_ostream buf(str);
|
2015-06-25 15:07:55 -04:00
|
|
|
if (!CheckSolenoidModule(m_moduleNumber)) {
|
2015-06-30 15:01:20 -04:00
|
|
|
buf << "Solenoid Module " << m_moduleNumber;
|
|
|
|
|
wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf.str());
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!CheckSolenoidChannel(m_channel)) {
|
2015-06-30 15:01:20 -04:00
|
|
|
buf << "Solenoid Module " << m_channel;
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2016-07-02 09:24:54 -07:00
|
|
|
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
m_solenoidHandle = HAL_InitializeSolenoidPort(
|
|
|
|
|
HAL_GetPortWithModule(moduleNumber, channel), &status);
|
2016-07-02 09:24:54 -07:00
|
|
|
if (status != 0) {
|
2016-08-12 13:45:28 -07:00
|
|
|
wpi_setErrorWithContextRange(status, 0, HAL_GetNumSolenoidChannels(),
|
|
|
|
|
channel, HAL_GetErrorMessage(status));
|
2016-07-09 00:24:26 -07:00
|
|
|
m_solenoidHandle = HAL_kInvalidHandle;
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
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("Solenoid", m_moduleNumber, m_channel,
|
2015-06-25 15:07:55 -04:00
|
|
|
this);
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_channel,
|
|
|
|
|
m_moduleNumber);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Solenoid::~Solenoid() {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_FreeSolenoidPort(m_solenoidHandle);
|
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 value of a solenoid.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param on Turn the solenoid output off or on.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Solenoid::Set(bool on) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
2016-07-02 09:24:54 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetSolenoid(m_solenoidHandle, on, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the current value of the solenoid.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return The current value of the solenoid.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Solenoid::Get() const {
|
|
|
|
|
if (StatusIsFatal()) return false;
|
2016-07-02 09:24:54 -07:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
bool value = HAL_GetSolenoid(m_solenoidHandle, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2016-07-02 09:24:54 -07:00
|
|
|
return value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
|
2014-12-26 19:40:39 -05:00
|
|
|
/**
|
|
|
|
|
* Check if solenoid is blacklisted.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* If a solenoid is shorted, it is added to the blacklist and
|
|
|
|
|
* disabled until power cycle, or until faults are cleared.
|
|
|
|
|
*
|
|
|
|
|
* @see ClearAllPCMStickyFaults()
|
2014-12-26 19:40:39 -05:00
|
|
|
*
|
|
|
|
|
* @return If solenoid is disabled due to short.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Solenoid::IsBlackListed() const {
|
|
|
|
|
int value = GetPCMSolenoidBlackList(m_moduleNumber) & (1 << m_channel);
|
|
|
|
|
return (value != 0);
|
2014-12-26 19:40:39 -05:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-08-13 23:17:19 -07:00
|
|
|
void Solenoid::ValueChanged(ITable* source, llvm::StringRef key,
|
|
|
|
|
std::shared_ptr<nt::Value> value, bool isNew) {
|
|
|
|
|
if (!value->IsBoolean()) return;
|
|
|
|
|
Set(value->GetBoolean());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solenoid::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutBoolean("Value", Get());
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solenoid::StartLiveWindowMode() {
|
2015-06-25 15:07:55 -04:00
|
|
|
Set(false);
|
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 Solenoid::StopLiveWindowMode() {
|
2015-06-25 15:07:55 -04:00
|
|
|
Set(false);
|
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 Solenoid::GetSmartDashboardType() const { return "Solenoid"; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-07-29 16:48:04 -04:00
|
|
|
void Solenoid::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> Solenoid::GetTable() const { return m_table; }
|