2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2008-2017 FIRST. 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
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "DoubleSolenoid.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
#include <HAL/Ports.h>
|
|
|
|
|
#include <HAL/Solenoid.h>
|
|
|
|
|
#include <llvm/SmallString.h>
|
|
|
|
|
#include <llvm/raw_ostream.h>
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "LiveWindow/LiveWindow.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "WPIErrors.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.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* Uses the default PCM ID of 0.
|
|
|
|
|
*
|
2015-06-29 02:43:44 -07:00
|
|
|
* @param forwardChannel The forward channel number on the PCM (0..7).
|
|
|
|
|
* @param reverseChannel The reverse channel number on the PCM (0..7).
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
DoubleSolenoid::DoubleSolenoid(int forwardChannel, int reverseChannel)
|
2016-05-20 17:30:37 -07:00
|
|
|
: DoubleSolenoid(GetDefaultSolenoidModule(), forwardChannel,
|
|
|
|
|
reverseChannel) {}
|
2015-06-29 02:43:44 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param moduleNumber The CAN ID of the PCM.
|
2015-06-29 02:43:44 -07:00
|
|
|
* @param forwardChannel The forward channel on the PCM to control (0..7).
|
|
|
|
|
* @param reverseChannel The reverse channel on the PCM to control (0..7).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
DoubleSolenoid::DoubleSolenoid(int moduleNumber, int forwardChannel,
|
|
|
|
|
int reverseChannel)
|
2015-06-29 02:43:44 -07:00
|
|
|
: SolenoidBase(moduleNumber),
|
|
|
|
|
m_forwardChannel(forwardChannel),
|
|
|
|
|
m_reverseChannel(reverseChannel) {
|
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_forwardChannel)) {
|
2015-06-30 15:01:20 -04:00
|
|
|
buf << "Solenoid Module " << m_forwardChannel;
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!CheckSolenoidChannel(m_reverseChannel)) {
|
2015-06-30 15:01:20 -04:00
|
|
|
buf << "Solenoid Module " << m_reverseChannel;
|
|
|
|
|
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_forwardHandle = HAL_InitializeSolenoidPort(
|
|
|
|
|
HAL_GetPortWithModule(moduleNumber, m_forwardChannel), &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(),
|
2016-07-13 20:29:28 -07:00
|
|
|
forwardChannel, 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
|
|
|
m_reverseHandle = HAL_InitializeSolenoidPort(
|
|
|
|
|
HAL_GetPortWithModule(moduleNumber, m_reverseChannel), &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(),
|
2016-07-13 20:29:28 -07:00
|
|
|
reverseChannel, HAL_GetErrorMessage(status));
|
2016-07-02 09:24:54 -07:00
|
|
|
// free forward solenoid
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_FreeSolenoidPort(m_forwardHandle);
|
|
|
|
|
m_forwardHandle = HAL_kInvalidHandle;
|
|
|
|
|
m_reverseHandle = HAL_kInvalidHandle;
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_forwardMask = 1 << m_forwardChannel;
|
|
|
|
|
m_reverseMask = 1 << m_reverseChannel;
|
2016-07-02 09:24:54 -07:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_forwardChannel,
|
|
|
|
|
m_moduleNumber);
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_reverseChannel,
|
|
|
|
|
m_moduleNumber);
|
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("DoubleSolenoid", m_moduleNumber,
|
2015-06-25 15:07:55 -04:00
|
|
|
m_forwardChannel, this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
DoubleSolenoid::~DoubleSolenoid() {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_FreeSolenoidPort(m_forwardHandle);
|
|
|
|
|
HAL_FreeSolenoidPort(m_reverseHandle);
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueListener != 0) m_valueEntry.RemoveListener(m_valueListener);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the value of a solenoid.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param value The value to set (Off, Forward or Reverse)
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void DoubleSolenoid::Set(Value value) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
|
2016-07-02 09:24:54 -07:00
|
|
|
bool forward = false;
|
|
|
|
|
bool reverse = false;
|
2015-06-25 15:07:55 -04:00
|
|
|
switch (value) {
|
|
|
|
|
case kOff:
|
2016-07-02 09:24:54 -07:00
|
|
|
forward = false;
|
|
|
|
|
reverse = false;
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
|
|
|
|
case kForward:
|
2016-07-02 09:24:54 -07:00
|
|
|
forward = true;
|
|
|
|
|
reverse = false;
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
|
|
|
|
case kReverse:
|
2016-07-02 09:24:54 -07:00
|
|
|
forward = false;
|
|
|
|
|
reverse = true;
|
2015-06-25 15:07:55 -04:00
|
|
|
break;
|
|
|
|
|
}
|
2016-09-06 00:01:45 -07:00
|
|
|
int fstatus = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetSolenoid(m_forwardHandle, forward, &fstatus);
|
2016-09-06 00:01:45 -07:00
|
|
|
int rstatus = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetSolenoid(m_reverseHandle, reverse, &rstatus);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(fstatus, HAL_GetErrorMessage(fstatus));
|
|
|
|
|
wpi_setErrorWithContext(rstatus, HAL_GetErrorMessage(rstatus));
|
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
|
|
|
DoubleSolenoid::Value DoubleSolenoid::Get() const {
|
|
|
|
|
if (StatusIsFatal()) return kOff;
|
2016-09-06 00:01:45 -07:00
|
|
|
int fstatus = 0;
|
|
|
|
|
int rstatus = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
bool valueForward = HAL_GetSolenoid(m_forwardHandle, &fstatus);
|
|
|
|
|
bool valueReverse = HAL_GetSolenoid(m_reverseHandle, &rstatus);
|
2016-07-02 09:24:54 -07:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(fstatus, HAL_GetErrorMessage(fstatus));
|
|
|
|
|
wpi_setErrorWithContext(rstatus, HAL_GetErrorMessage(rstatus));
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-02 09:24:54 -07:00
|
|
|
if (valueForward) return kForward;
|
|
|
|
|
if (valueReverse) return kReverse;
|
2015-06-25 15:07:55 -04:00
|
|
|
return kOff;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2017-11-08 23:44:03 -08:00
|
|
|
|
2014-12-26 19:40:39 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the forward 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 DoubleSolenoid::IsFwdSolenoidBlackListed() const {
|
|
|
|
|
int blackList = GetPCMSolenoidBlackList(m_moduleNumber);
|
2017-11-08 23:44:03 -08:00
|
|
|
return (blackList & m_forwardMask) != 0;
|
2014-12-26 19:40:39 -05:00
|
|
|
}
|
2017-11-08 23:44:03 -08:00
|
|
|
|
2014-12-26 19:40:39 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the reverse 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.
|
2014-12-26 19:40:39 -05:00
|
|
|
*
|
2017-11-16 00:33:51 -08:00
|
|
|
* @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 DoubleSolenoid::IsRevSolenoidBlackListed() const {
|
|
|
|
|
int blackList = GetPCMSolenoidBlackList(m_moduleNumber);
|
2017-11-08 23:44:03 -08:00
|
|
|
return (blackList & m_reverseMask) != 0;
|
2014-12-26 19:40:39 -05:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
void DoubleSolenoid::UpdateTable() {
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueEntry) {
|
|
|
|
|
switch (Get()) {
|
|
|
|
|
case kForward:
|
|
|
|
|
m_valueEntry.SetString("Forward");
|
|
|
|
|
break;
|
|
|
|
|
case kReverse:
|
|
|
|
|
m_valueEntry.SetString("Reverse");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
m_valueEntry.SetString("Off");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DoubleSolenoid::StartLiveWindowMode() {
|
2015-06-25 15:07:55 -04:00
|
|
|
Set(kOff);
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueEntry) {
|
|
|
|
|
m_valueListener = m_valueEntry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsString()) return;
|
|
|
|
|
Value lvalue = kOff;
|
|
|
|
|
if (event.value->GetString() == "Forward")
|
|
|
|
|
lvalue = kForward;
|
|
|
|
|
else if (event.value->GetString() == "Reverse")
|
|
|
|
|
lvalue = kReverse;
|
|
|
|
|
Set(lvalue);
|
|
|
|
|
},
|
|
|
|
|
NT_NOTIFY_IMMEDIATE | NT_NOTIFY_NEW | NT_NOTIFY_UPDATE);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DoubleSolenoid::StopLiveWindowMode() {
|
2015-06-25 15:07:55 -04:00
|
|
|
Set(kOff);
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueListener != 0) {
|
|
|
|
|
m_valueEntry.RemoveListener(m_valueListener);
|
|
|
|
|
m_valueListener = 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
std::string DoubleSolenoid::GetSmartDashboardType() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
return "Double Solenoid";
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
void DoubleSolenoid::InitTable(std::shared_ptr<nt::NetworkTable> subTable) {
|
2017-09-02 00:35:30 -07:00
|
|
|
if (subTable) {
|
|
|
|
|
m_valueEntry = subTable->GetEntry("Value");
|
2017-09-02 00:17:43 -07:00
|
|
|
UpdateTable();
|
|
|
|
|
} else {
|
|
|
|
|
m_valueEntry = nt::NetworkTableEntry();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|