2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2014-08-05 15:21:12 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* 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 "DoubleSolenoid.h"
|
2014-01-06 10:12:21 -05:00
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include "LiveWindow/LiveWindow.h"
|
|
|
|
|
|
|
|
|
|
/**
|
2014-12-29 14:09:37 -05:00
|
|
|
* Common function to implement constructor behaviour.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void DoubleSolenoid::InitSolenoid() {
|
|
|
|
|
char buf[64];
|
|
|
|
|
if (!CheckSolenoidModule(m_moduleNumber)) {
|
|
|
|
|
snprintf(buf, 64, "Solenoid Module %d", m_moduleNumber);
|
|
|
|
|
wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!CheckSolenoidChannel(m_forwardChannel)) {
|
|
|
|
|
snprintf(buf, 64, "Solenoid Channel %d", m_forwardChannel);
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!CheckSolenoidChannel(m_reverseChannel)) {
|
|
|
|
|
snprintf(buf, 64, "Solenoid Channel %d", m_reverseChannel);
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Resource::CreateResourceObject(
|
|
|
|
|
&m_allocated, solenoid_kNumDO7_0Elements * kSolenoidChannels);
|
|
|
|
|
|
|
|
|
|
snprintf(buf, 64, "Solenoid %d (Module: %d)", m_forwardChannel,
|
|
|
|
|
m_moduleNumber);
|
|
|
|
|
if (m_allocated->Allocate(
|
|
|
|
|
m_moduleNumber * kSolenoidChannels + m_forwardChannel, buf) == ~0ul) {
|
|
|
|
|
CloneError(m_allocated);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snprintf(buf, 64, "Solenoid %d (Module: %d)", m_reverseChannel,
|
|
|
|
|
m_moduleNumber);
|
|
|
|
|
if (m_allocated->Allocate(
|
|
|
|
|
m_moduleNumber * kSolenoidChannels + m_reverseChannel, buf) == ~0ul) {
|
|
|
|
|
CloneError(m_allocated);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_forwardMask = 1 << m_forwardChannel;
|
|
|
|
|
m_reverseMask = 1 << m_reverseChannel;
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Solenoid, m_forwardChannel,
|
|
|
|
|
m_moduleNumber);
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Solenoid, m_reverseChannel,
|
|
|
|
|
m_moduleNumber);
|
|
|
|
|
LiveWindow::GetInstance()->AddActuator("DoubleSolenoid", m_moduleNumber,
|
|
|
|
|
m_forwardChannel, this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
2014-12-29 14:09:37 -05:00
|
|
|
* Uses the default PCM ID of 0
|
2015-01-07 20:15:26 -08:00
|
|
|
* @param forwardChannel The forward channel number on the PCM (0..7).
|
|
|
|
|
* @param reverseChannel The reverse channel number on the PCM (0..7).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
|
|
|
|
DoubleSolenoid::DoubleSolenoid(uint32_t forwardChannel, uint32_t reverseChannel)
|
2015-06-25 15:07:55 -04:00
|
|
|
: SolenoidBase(GetDefaultSolenoidModule()),
|
|
|
|
|
m_forwardChannel(forwardChannel),
|
|
|
|
|
m_reverseChannel(reverseChannel) {
|
|
|
|
|
InitSolenoid();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2014-08-05 14:42:37 -04:00
|
|
|
* @param moduleNumber The CAN ID of the PCM.
|
2015-01-07 20:15:26 -08: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
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
DoubleSolenoid::DoubleSolenoid(uint8_t moduleNumber, uint32_t forwardChannel,
|
|
|
|
|
uint32_t reverseChannel)
|
|
|
|
|
: SolenoidBase(moduleNumber),
|
|
|
|
|
m_forwardChannel(forwardChannel),
|
|
|
|
|
m_reverseChannel(reverseChannel) {
|
|
|
|
|
InitSolenoid();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
DoubleSolenoid::~DoubleSolenoid() {
|
|
|
|
|
if (CheckSolenoidModule(m_moduleNumber)) {
|
|
|
|
|
m_allocated->Free(m_moduleNumber * kSolenoidChannels + m_forwardChannel);
|
|
|
|
|
m_allocated->Free(m_moduleNumber * kSolenoidChannels + m_reverseChannel);
|
|
|
|
|
}
|
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;
|
|
|
|
|
uint8_t rawValue = 0x00;
|
|
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
|
case kOff:
|
|
|
|
|
rawValue = 0x00;
|
|
|
|
|
break;
|
|
|
|
|
case kForward:
|
|
|
|
|
rawValue = m_forwardMask;
|
|
|
|
|
break;
|
|
|
|
|
case kReverse:
|
|
|
|
|
rawValue = m_reverseMask;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SolenoidBase::Set(rawValue, m_forwardMask | m_reverseMask, m_moduleNumber);
|
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;
|
|
|
|
|
uint8_t value = GetAll(m_moduleNumber);
|
|
|
|
|
|
|
|
|
|
if (value & m_forwardMask) return kForward;
|
|
|
|
|
if (value & m_reverseMask) return kReverse;
|
|
|
|
|
return kOff;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-12-26 19:40:39 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the forward solenoid is blacklisted.
|
2015-06-25 15:07:55 -04: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);
|
|
|
|
|
return (blackList & m_forwardMask) ? 1 : 0;
|
2014-12-26 19:40:39 -05:00
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Check if the reverse solenoid is blacklisted.
|
2015-06-25 15:07:55 -04: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::IsRevSolenoidBlackListed() const {
|
|
|
|
|
int blackList = GetPCMSolenoidBlackList(m_moduleNumber);
|
|
|
|
|
return (blackList & m_reverseMask) ? 1 : 0;
|
2014-12-26 19:40:39 -05:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void DoubleSolenoid::ValueChanged(ITable *source, const std::string &key,
|
|
|
|
|
EntryValue value, bool isNew) {
|
|
|
|
|
Value lvalue = kOff;
|
|
|
|
|
std::string *val = (std::string *)value.ptr;
|
|
|
|
|
if (*val == "Forward")
|
|
|
|
|
lvalue = kForward;
|
|
|
|
|
else if (*val == "Reverse")
|
|
|
|
|
lvalue = kReverse;
|
|
|
|
|
Set(lvalue);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DoubleSolenoid::UpdateTable() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (m_table != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table->PutString(
|
|
|
|
|
"Value", (Get() == kForward ? "Forward"
|
|
|
|
|
: (Get() == kReverse ? "Reverse" : "Off")));
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DoubleSolenoid::StartLiveWindowMode() {
|
2015-06-25 15:07:55 -04:00
|
|
|
Set(kOff);
|
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 DoubleSolenoid::StopLiveWindowMode() {
|
2015-06-25 15:07:55 -04:00
|
|
|
Set(kOff);
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DoubleSolenoid::InitTable(ITable *subTable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ITable *DoubleSolenoid::GetTable() const { return m_table; }
|