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 "Solenoid.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 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);
|
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
|
|
|
*
|
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
|
|
|
|
2017-11-26 12:55:21 -08:00
|
|
|
/**
|
|
|
|
|
* Set the pulse duration in the PCM. This is used in conjunction with
|
|
|
|
|
* the startPulse method to allow the PCM to control the timing of a pulse.
|
|
|
|
|
* The timing can be controlled in 0.01 second increments.
|
|
|
|
|
*
|
|
|
|
|
* @param durationSeconds The duration of the pulse, from 0.01 to 2.55 seconds.
|
|
|
|
|
*
|
|
|
|
|
* @see startPulse()
|
|
|
|
|
*/
|
|
|
|
|
void Solenoid::SetPulseDuration(double durationSeconds) {
|
|
|
|
|
int32_t durationMS = durationSeconds * 1000;
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetOneShotDuration(m_solenoidHandle, durationMS, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Trigger the PCM to generate a pulse of the duration set in
|
|
|
|
|
* setPulseDuration.
|
|
|
|
|
*
|
|
|
|
|
* @see setPulseDuration()
|
|
|
|
|
*/
|
|
|
|
|
void Solenoid::StartPulse() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_FireOneShot(m_solenoidHandle, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
void Solenoid::UpdateTable() {
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueEntry) m_valueEntry.SetBoolean(Get());
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solenoid::StartLiveWindowMode() {
|
2015-06-25 15:07:55 -04:00
|
|
|
Set(false);
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_valueEntry) {
|
|
|
|
|
m_valueEntry.AddListener(
|
|
|
|
|
[=](const nt::EntryNotification& event) {
|
|
|
|
|
if (!event.value->IsBoolean()) return;
|
|
|
|
|
Set(event.value->GetBoolean());
|
|
|
|
|
},
|
|
|
|
|
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 Solenoid::StopLiveWindowMode() {
|
2015-06-25 15:07:55 -04:00
|
|
|
Set(false);
|
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
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string Solenoid::GetSmartDashboardType() const { return "Solenoid"; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
void Solenoid::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
|
|
|
}
|