2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 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>
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
#include "SensorBase.h"
|
|
|
|
|
#include "SmartDashboard/SendableBuilder.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)
|
2017-12-04 23:28:33 -08:00
|
|
|
: Solenoid(SensorBase::GetDefaultSolenoidModule(), channel) {}
|
2015-06-29 02:43:44 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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-12-04 23:28:33 -08:00
|
|
|
if (!SensorBase::CheckSolenoidModule(m_moduleNumber)) {
|
2018-05-13 17:09:56 -07:00
|
|
|
wpi_setWPIErrorWithContext(ModuleIndexOutOfRange,
|
|
|
|
|
"Solenoid Module " + wpi::Twine(m_moduleNumber));
|
2015-06-25 15:07:55 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
if (!SensorBase::CheckSolenoidChannel(m_channel)) {
|
2017-12-01 21:50:24 -08:00
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange,
|
2018-04-29 23:33:19 -07:00
|
|
|
"Solenoid Channel " + wpi::Twine(m_channel));
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_channel,
|
|
|
|
|
m_moduleNumber);
|
2017-12-04 23:28:33 -08:00
|
|
|
SetName("Solenoid", m_moduleNumber, m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destructor.
|
|
|
|
|
*/
|
2017-12-04 23:28:33 -08:00
|
|
|
Solenoid::~Solenoid() { HAL_FreeSolenoidPort(m_solenoidHandle); }
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void Solenoid::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Solenoid");
|
|
|
|
|
builder.SetSafeState([=]() { Set(false); });
|
|
|
|
|
builder.AddBooleanProperty("Value", [=]() { return Get(); },
|
|
|
|
|
[=](bool value) { Set(value); });
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|