2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this 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>
|
|
|
|
|
|
2021-06-05 22:36:39 -07:00
|
|
|
#include <wpi/NullDeleter.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
|
|
|
|
#include <wpi/sendable/SendableRegistry.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SensorUtil.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2025-02-25 19:07:01 -08:00
|
|
|
Solenoid::Solenoid(int busId, int module, PneumaticsModuleType moduleType,
|
|
|
|
|
int channel)
|
|
|
|
|
: m_module{PneumaticsBase::GetForType(busId, module, moduleType)},
|
2021-09-16 18:50:27 -07:00
|
|
|
m_channel{channel} {
|
|
|
|
|
if (!m_module->CheckSolenoidChannel(m_channel)) {
|
|
|
|
|
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", m_channel);
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2021-06-05 22:36:39 -07:00
|
|
|
m_mask = 1 << channel;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2021-07-09 15:11:12 -07:00
|
|
|
if (m_module->CheckAndReserveSolenoids(m_mask) != 0) {
|
|
|
|
|
throw FRC_MakeError(err::ResourceAlreadyAllocated, "Channel {}", m_channel);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 12:37:23 -08:00
|
|
|
m_module->ReportUsage(fmt::format("Solenoid[{}]", m_channel), "Solenoid");
|
2025-01-25 10:52:19 -08:00
|
|
|
wpi::SendableRegistry::Add(this, "Solenoid", m_module->GetModuleNumber(),
|
|
|
|
|
m_channel);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-25 19:07:01 -08:00
|
|
|
Solenoid::Solenoid(int busId, PneumaticsModuleType moduleType, int channel)
|
|
|
|
|
: Solenoid{busId, PneumaticsBase::GetDefaultForType(moduleType), moduleType,
|
2021-09-16 18:50:27 -07:00
|
|
|
channel} {}
|
|
|
|
|
|
2021-07-09 15:11:12 -07:00
|
|
|
Solenoid::~Solenoid() {
|
2022-03-01 11:10:45 -08:00
|
|
|
if (m_module) {
|
|
|
|
|
m_module->UnreserveSolenoids(m_mask);
|
|
|
|
|
}
|
2021-07-09 15:11:12 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Solenoid::Set(bool on) {
|
2021-06-05 22:36:39 -07:00
|
|
|
int value = on ? (0xFFFF & m_mask) : 0;
|
|
|
|
|
m_module->SetSolenoids(m_mask, value);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Solenoid::Get() const {
|
2021-06-05 22:36:39 -07:00
|
|
|
int currentAll = m_module->GetSolenoids();
|
|
|
|
|
return (currentAll & m_mask) != 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
void Solenoid::Toggle() {
|
|
|
|
|
Set(!Get());
|
|
|
|
|
}
|
2020-08-15 08:16:32 -07:00
|
|
|
|
2021-02-17 04:03:57 +02:00
|
|
|
int Solenoid::GetChannel() const {
|
|
|
|
|
return m_channel;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 22:36:39 -07:00
|
|
|
bool Solenoid::IsDisabled() const {
|
|
|
|
|
return (m_module->GetSolenoidDisabledList() & m_mask) != 0;
|
2014-12-26 19:40:39 -05:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
void Solenoid::SetPulseDuration(units::second_t duration) {
|
2021-06-05 22:36:39 -07:00
|
|
|
m_module->SetOneShotDuration(m_channel, duration);
|
2017-11-26 12:55:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Solenoid::StartPulse() {
|
2021-06-05 22:36:39 -07:00
|
|
|
m_module->FireOneShot(m_channel);
|
2017-11-26 12:55:21 -08:00
|
|
|
}
|
|
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void Solenoid::InitSendable(wpi::SendableBuilder& builder) {
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.SetSmartDashboardType("Solenoid");
|
2018-07-28 14:04:46 -07:00
|
|
|
builder.SetActuator(true);
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddBooleanProperty(
|
2022-10-15 16:33:14 -07:00
|
|
|
"Value", [=, this] { return Get(); },
|
|
|
|
|
[=, this](bool value) { Set(value); });
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|