blob: 5174f987e0ac6aa55c8fd1f02ec0082e25fdf433 [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008. All Rights Reserved.
3 */
4/* Open Source Software - may be modified and shared by FRC teams. The code */
5/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
6/*----------------------------------------------------------------------------*/
7
8#include "Solenoid.h"
9#include "WPIErrors.h"
10#include "LiveWindow/LiveWindow.h"
11
12#include <sstream>
13
14/**
15 * Constructor using the default PCM ID (0).
16 *
17 * @param channel The channel on the PCM to control (0..7).
18 */
19Solenoid::Solenoid(uint32_t channel)
20 : Solenoid(GetDefaultSolenoidModule(), channel) {}
21
22/**
23 * Constructor.
24 *
25 * @param moduleNumber The CAN ID of the PCM the solenoid is attached to
26 * @param channel The channel on the PCM to control (0..7).
27 */
28Solenoid::Solenoid(uint8_t moduleNumber, uint32_t channel)
29 : SolenoidBase(moduleNumber), m_channel(channel) {
30 std::stringstream buf;
31 if (!CheckSolenoidModule(m_moduleNumber)) {
32 buf << "Solenoid Module " << m_moduleNumber;
33 wpi_setWPIErrorWithContext(ModuleIndexOutOfRange, buf.str());
34 return;
35 }
36 if (!CheckSolenoidChannel(m_channel)) {
37 buf << "Solenoid Module " << m_channel;
38 wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
39 return;
40 }
41 Resource::CreateResourceObject(m_allocated, m_maxModules * m_maxPorts);
42 buf << "Solenoid " << m_channel << " (Module: " << m_moduleNumber << ")";
43 if (m_allocated->Allocate(m_moduleNumber * kSolenoidChannels + m_channel,
44 buf.str()) ==
45 std::numeric_limits<uint32_t>::max()) {
46 CloneError(*m_allocated);
47 return;
48 }
49
50 LiveWindow::GetInstance()->AddActuator("Solenoid", m_moduleNumber, m_channel,
51 this);
52 HALReport(HALUsageReporting::kResourceType_Solenoid, m_channel,
53 m_moduleNumber);
54}
55
56/**
57 * Destructor.
58 */
59Solenoid::~Solenoid() {
60 if (CheckSolenoidModule(m_moduleNumber)) {
61 m_allocated->Free(m_moduleNumber * kSolenoidChannels + m_channel);
62 }
63 if (m_table != nullptr) m_table->RemoveTableListener(this);
64}
65
66/**
67 * Set the value of a solenoid.
68 *
69 * @param on Turn the solenoid output off or on.
70 */
71void Solenoid::Set(bool on) {
72 if (StatusIsFatal()) return;
73 uint8_t value = on ? 0xFF : 0x00;
74 uint8_t mask = 1 << m_channel;
75
76 SolenoidBase::Set(value, mask, m_moduleNumber);
77}
78
79/**
80 * Read the current value of the solenoid.
81 *
82 * @return The current value of the solenoid.
83 */
84bool Solenoid::Get() const {
85 if (StatusIsFatal()) return false;
86 uint8_t value = GetAll(m_moduleNumber) & (1 << m_channel);
87 return (value != 0);
88}
89/**
90 * Check if solenoid is blacklisted.
91 * If a solenoid is shorted, it is added to the blacklist and
92 * disabled until power cycle, or until faults are cleared.
93 * @see ClearAllPCMStickyFaults()
94 *
95 * @return If solenoid is disabled due to short.
96 */
97bool Solenoid::IsBlackListed() const {
98 int value = GetPCMSolenoidBlackList(m_moduleNumber) & (1 << m_channel);
99 return (value != 0);
100}
101
102void Solenoid::ValueChanged(ITable* source, llvm::StringRef key,
103 std::shared_ptr<nt::Value> value, bool isNew) {
104 if (!value->IsBoolean()) return;
105 Set(value->GetBoolean());
106}
107
108void Solenoid::UpdateTable() {
109 if (m_table != nullptr) {
110 m_table->PutBoolean("Value", Get());
111 }
112}
113
114void Solenoid::StartLiveWindowMode() {
115 Set(false);
116 if (m_table != nullptr) {
117 m_table->AddTableListener("Value", this, true);
118 }
119}
120
121void Solenoid::StopLiveWindowMode() {
122 Set(false);
123 if (m_table != nullptr) {
124 m_table->RemoveTableListener(this);
125 }
126}
127
128std::string Solenoid::GetSmartDashboardType() const { return "Solenoid"; }
129
130void Solenoid::InitTable(std::shared_ptr<ITable> subTable) {
131 m_table = subTable;
132 UpdateTable();
133}
134
135std::shared_ptr<ITable> Solenoid::GetTable() const { return m_table; }