2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -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 "Solenoid.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"
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Common function to implement constructor behavior.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Solenoid::InitSolenoid() {
|
|
|
|
|
char buf[64];
|
|
|
|
|
if (!CheckSolenoidModule(m_moduleNumber)) {
|
|
|
|
|
snprintf(buf, 64, "Solenoid Module %d", m_moduleNumber);
|
|
|
|
|
wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!CheckSolenoidChannel(m_channel)) {
|
|
|
|
|
snprintf(buf, 64, "Solenoid Channel %d", m_channel);
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Resource::CreateResourceObject(&m_allocated, kSolenoidChannels * 63);
|
|
|
|
|
|
|
|
|
|
snprintf(buf, 64, "Solenoid %d (Module: %d)", m_channel, m_moduleNumber);
|
|
|
|
|
if (m_allocated->Allocate(m_moduleNumber * kSolenoidChannels + m_channel,
|
|
|
|
|
buf) == ~0ul) {
|
|
|
|
|
CloneError(m_allocated);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LiveWindow::GetInstance()->AddActuator("Solenoid", m_moduleNumber, m_channel,
|
|
|
|
|
this);
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Solenoid, m_channel,
|
|
|
|
|
m_moduleNumber);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-12-29 14:09:37 -05:00
|
|
|
* Constructor using the default PCM ID (0).
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param channel The channel on the PCM to control (0..7).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
|
|
|
|
Solenoid::Solenoid(uint32_t channel)
|
2015-06-25 15:07:55 -04:00
|
|
|
: SolenoidBase(GetDefaultSolenoidModule()), m_channel(channel) {
|
|
|
|
|
InitSolenoid();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @param moduleNumber The CAN ID of the PCM the solenoid is attached to
|
|
|
|
|
* @param channel The channel on the PCM to control (0..7).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
|
|
|
|
Solenoid::Solenoid(uint8_t moduleNumber, uint32_t channel)
|
2015-06-25 15:07:55 -04:00
|
|
|
: SolenoidBase(moduleNumber), m_channel(channel) {
|
|
|
|
|
InitSolenoid();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Solenoid::~Solenoid() {
|
|
|
|
|
if (CheckSolenoidModule(m_moduleNumber)) {
|
|
|
|
|
m_allocated->Free(m_moduleNumber * kSolenoidChannels + m_channel);
|
|
|
|
|
}
|
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;
|
|
|
|
|
uint8_t value = on ? 0xFF : 0x00;
|
|
|
|
|
uint8_t mask = 1 << m_channel;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
SolenoidBase::Set(value, mask, 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
|
|
|
bool Solenoid::Get() const {
|
|
|
|
|
if (StatusIsFatal()) return false;
|
|
|
|
|
uint8_t value = GetAll(m_moduleNumber) & (1 << m_channel);
|
|
|
|
|
return (value != 0);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-12-26 19:40:39 -05:00
|
|
|
/**
|
|
|
|
|
* Check if 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 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-06-25 15:07:55 -04:00
|
|
|
void Solenoid::ValueChanged(ITable* source, const std::string& key,
|
|
|
|
|
EntryValue value, bool isNew) {
|
|
|
|
|
Set(value.b);
|
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-06-25 15:07:55 -04:00
|
|
|
void Solenoid::InitTable(ITable* subTable) {
|
|
|
|
|
m_table = subTable;
|
|
|
|
|
UpdateTable();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
ITable* Solenoid::GetTable() const { return m_table; }
|