2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-08-25 18:42:00 -07:00
|
|
|
/* Copyright (c) 2008-2019 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
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Solenoid.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
|
|
|
|
#include <hal/Ports.h>
|
|
|
|
|
#include <hal/Solenoid.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SensorUtil.h"
|
|
|
|
|
#include "frc/WPIErrors.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
2019-09-14 15:22:54 -05:00
|
|
|
#include "frc/smartdashboard/SendableRegistry.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
Solenoid::Solenoid(int channel)
|
2018-05-23 20:22:30 -07:00
|
|
|
: Solenoid(SensorUtil::GetDefaultSolenoidModule(), channel) {}
|
2015-06-29 02:43:44 -07: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) {
|
2018-05-23 20:22:30 -07:00
|
|
|
if (!SensorUtil::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;
|
|
|
|
|
}
|
2018-05-23 20:22:30 -07:00
|
|
|
if (!SensorUtil::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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-29 21:34:10 -07:00
|
|
|
HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_channel + 1,
|
|
|
|
|
m_moduleNumber + 1);
|
2019-09-14 15:22:54 -05:00
|
|
|
SendableRegistry::GetInstance().AddLW(this, "Solenoid", m_moduleNumber,
|
|
|
|
|
m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
Solenoid::~Solenoid() { HAL_FreeSolenoidPort(m_solenoidHandle); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
2018-07-28 14:04:46 -07:00
|
|
|
builder.SetActuator(true);
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSafeState([=]() { Set(false); });
|
|
|
|
|
builder.AddBooleanProperty("Value", [=]() { return Get(); },
|
|
|
|
|
[=](bool value) { Set(value); });
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|